3318. Find X-Sum of All K-Long Subarrays I
Description
You are given an array nums
of n
integers and two integers k
and x
.
The x-sum of an array is calculated by the following procedure:
- Count the occurrences of all elements in the array.
- Keep only the occurrences of the top
x
most frequent elements. If two elements have the same number of occurrences, the element with the bigger value is considered more frequent. - Calculate the sum of the resulting array.
Note that if an array has less than x
distinct elements, its x-sum is the sum of the array.
Return an integer array answer
of length n - k + 1
where answer[i]
is the x-sum of the subarray nums[i..i + k - 1]
.
Example 1:
Input: nums = [1,1,2,2,3,4,2,3], k = 6, x = 2
Output: [6,10,12]
Explanation:
- For subarray
[1, 1, 2, 2, 3, 4]
, only elements 1 and 2 will be kept in the resulting array. Hence,answer[0] = 1 + 1 + 2 + 2
. - For subarray
[1, 2, 2, 3, 4, 2]
, only elements 2 and 4 will be kept in the resulting array. Hence,answer[1] = 2 + 2 + 2 + 4
. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times. - For subarray
[2, 2, 3, 4, 2, 3]
, only elements 2 and 3 are kept in the resulting array. Hence,answer[2] = 2 + 2 + 2 + 3 + 3
.
Example 2:
Input: nums = [3,8,7,8,7,5], k = 2, x = 2
Output: [11,15,15,15,12]
Explanation:
Since k == x
, answer[i]
is equal to the sum of the subarray nums[i..i + k - 1]
.
Constraints:
1 <= n == nums.length <= 50
1 <= nums[i] <= 50
1 <= x <= k <= nums.length
Solutions
Solution 1: Hash Table + Ordered Set
We use a hash table \(\textit{cnt}\) to count the occurrences of each element in the window, an ordered set \(\textit{l}\) to store the \(x\) elements with the highest occurrences in the window, and another ordered set \(\textit{r}\) to store the remaining elements.
We maintain a variable \(\textit{s}\) to represent the sum of the elements in \(\textit{l}\). Initially, we add the first \(k\) elements to the window, update the ordered sets \(\textit{l}\) and \(\textit{r}\), and calculate the value of \(\textit{s}\). If the size of \(\textit{l}\) is less than \(x\) and \(\textit{r}\) is not empty, we repeatedly move the largest element from \(\textit{r}\) to \(\textit{l}\) until the size of \(\textit{l}\) equals \(x\), updating the value of \(\textit{s}\) in the process. If the size of \(\textit{l}\) is greater than \(x\), we repeatedly move the smallest element from \(\textit{l}\) to \(\textit{r}\) until the size of \(\textit{l}\) equals \(x\), updating the value of \(\textit{s}\) in the process. At this point, we can calculate the current window's \(\textit{x-sum}\) and add it to the answer array. Then we remove the left boundary element of the window, update \(\textit{cnt}\), and update the ordered sets \(\textit{l}\) and \(\textit{r}\), as well as the value of \(\textit{s}\). Continue traversing the array until the traversal is complete.
The time complexity is \(O(n \times \log k)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(\textit{nums}\).
Similar problems:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
1 |
|