#3321
Find X-Sum of All K-Long Subarrays II
master · 1680 · lc hard +32 · 41.1% accepted · 448 likes · top 21%
Description
You are given an array nums of n integers along with two integers k and x.
The x-sum of an array is computed as follows:
- Count the occurrences of each distinct element.
- Keep only the top x most frequent values' contributions. When two values tie on frequency, the larger value ranks higher.
- Sum all kept elements.
If the array has fewer than x distinct values, the x-sum is the ordinary sum.
Return a long integer array answer of length n - k + 1 where answer[i] equals the x-sum of the window nums[i..i + k - 1].
Code
1
2
3