【发布时间】:2015-10-31 18:35:30
【问题描述】:
如何有效地解决这个问题?
给定一个大小为 n 的数组和一个整数 k,我们需要返回大小为 k 的窗口中所有不同数字的计数之和。窗口向前滑动。
例如arr[] = {1,2,1,3,4,2,3};
设 k = 4。
The first window is {1,2,1,3}, count of distinct numbers is 2….(1 is repeated)
The second window is {2,1,3,4} count of distinct numbers is 4
The third window is {1,3,4,2} count of distinct numbers is 4
The fourth window is {3,4,2,3} count of distinct numbers is 2
【问题讨论】:
-
元素的期望值范围是多少?如果范围适中,您可以使用数组来注册给定值的存在。
标签: arrays algorithm integer distinct-values