ClickHouse 数组 - 在数组中找到最长的重复数字链
Posted
技术标签:
【中文标题】ClickHouse 数组 - 在数组中找到最长的重复数字链【英文标题】:ClickHouse array - find a longest chain of repeating number in array 【发布时间】:2019-10-22 10:21:00 【问题描述】:在 Clickhouse 中,我有一列包含 Int16 元素的数组。我正在寻找一种方法来找到最长的重复数字 1 链。 例如,在数组 [0,1,1,1,5,1,1,1,1,1,2] 中,重复 1 的最长链是 5 个元素。有没有办法用现有的功能做到这一点?
【问题讨论】:
上面的“可能重复”是错误的。该链接适用于 javascript 中的出现。这里的 OP 在 Clickhouse 中提问。 【参考方案1】:试试这个查询:
SELECT
/* The source number. */
data.1 AS number,
/* The source array. */
data.2 AS array,
/* Number the values in each chain. */
arrayCumSumNonNegative((x, index) -> x = number ? 1 : -index, array, arrayEnumerate(array)) AS partiallySumArray,
arrayReduce('max', partiallySumArray) AS result
FROM
(
/* test data set */
SELECT arrayJoin([
/**/
(1, []),
(1, [0, 2, 2, 2, 5]),
(1, [0, 1, 1, 1, 5, 1, 1, 1, 1, 1,2]),
(1, [1, 1, 1, 2, 3, 4, 5, 1, 1]),
(1, [-5, 100, 1, 1, 0, 1, 1, 1]),
(1, [1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0]),
/**/
(5, []),
(5, [0, 2, 2, 2, 55]),
(5, [5, 5, 10, 300, 5, 77, 5])
]) AS data
)
FORMAT Vertical
/* Result:
Row 1:
──────
number: 1
array: []
partiallySumArray: []
result: 0
Row 2:
──────
number: 1
array: [0,2,2,2,5]
partiallySumArray: [0,0,0,0,0]
result: 0
Row 3:
──────
number: 1
array: [0,1,1,1,5,1,1,1,1,1,2]
partiallySumArray: [0,1,2,3,0,1,2,3,4,5,0]
result: 5
Row 4:
──────
number: 1
array: [1,1,1,2,3,4,5,1,1]
partiallySumArray: [1,2,3,0,0,0,0,1,2]
result: 3
Row 5:
──────
number: 1
array: [-5,100,1,1,0,1,1,1]
partiallySumArray: [0,0,1,2,0,1,2,3]
result: 3
Row 6:
──────
number: 1
array: [1,1,0,1,1,1,1,1,1,0,0]
partiallySumArray: [1,2,0,1,2,3,4,5,6,0,0]
result: 6
Row 7:
──────
number: 5
array: []
partiallySumArray: []
result: 0
Row 8:
──────
number: 5
array: [0,2,2,2,55]
partiallySumArray: [0,0,0,0,0]
result: 0
Row 9:
───────
number: 5
array: [5,5,10,300,5,77,5]
partiallySumArray: [1,2,0,0,1,0,1]
result: 2
*/
【讨论】:
很高兴为您提供帮助 ;)以上是关于ClickHouse 数组 - 在数组中找到最长的重复数字链的主要内容,如果未能解决你的问题,请参考以下文章
Clickhouse - 矩阵逐项加法:如何对二维数组求和?
2021-11-16:最长递增子序列的个数。给定一个未排序的整数数组,找到最长递增子序列的个数。注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。力扣673。