1300. Dominated Windows
A streaming service records the genre code of each song played, in the array nums. A block of exactly k consecutive songs is dominated when some single genre code fills more than half of the block, that is, it occurs strictly more than k / 2 times. Return the number of dominated blocks, where blocks are counted by their start position. For an odd k the threshold is a real number, so for example a block of 3 needs at least 2 equal codes.
Aim for O(n) time and O(n) space, where n is the length of nums: slide a count map together with a count-of-counts table, so the block's highest frequency is updated in O(1) per step.
Example 1
- Input:
- nums = [1,1,2,1,3], k = 3
- Output:
- 2
- Explanation:
The blocks [1,1,2] and [1,2,1] each have a value occurring twice, which is more than 1.5, while [2,1,3] has none, so 2.
Example 2
- Input:
- nums = [1,2,3,4], k = 2
- Output:
- 0
- Explanation:
A block of 2 is dominated only when both songs are equal (more than 1 occurrence), and no neighbours are equal, so 0.
Example 3
- Input:
- nums = [7,7,7], k = 1
- Output:
- 3
- Explanation:
With k = 1 every block holds one song that occurs once, which is more than 0.5, so all 3 blocks count.
Constraints
1 ≤ k ≤ nums.length ≤ 2 * 105
1 ≤ nums[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(n)