3672. Sum of Weighted Modes in Subarrays π
Description
You are given an integer array nums
and an integer k
.
For every subarray of length k
:
- The mode is defined as the element with the highest frequency. If there are multiple choices for a mode, the smallest such element is taken.
- The weight is defined as
mode * frequency(mode)
.
Return the sum of the weights of all subarrays of length k
.
Note:
- A subarray is a contiguous non-empty sequence of elements within an array.
- The frequency of an element
x
is the number of times it occurs in the array.
Example 1:
Input: nums = [1,2,2,3], k = 3
Output: 8
Explanation:
Subarrays of length k = 3
are:
Subarray | Frequencies | Mode | Mode βββββββFrequency |
Weight |
---|---|---|---|---|
[1, 2, 2] | 1: 1, 2: 2 | 2 | 2 | 2 × 2 = 4 |
[2, 2, 3] | 2: 2, 3: 1 | 2 | 2 | 2 × 2 = 4 |
Thus, the sum of weights is 4 + 4 = 8
.
Example 2:
Input: nums = [1,2,1,2], k = 2
Output: 3
Explanation:
Subarrays of length k = 2
are:
Subarray | Frequencies | Mode | Mode Frequency |
Weight |
---|---|---|---|---|
[1, 2] | 1: 1, 2: 1 | 1 | 1 | 1 × 1 = 1 |
[2, 1] | 2: 1, 1: 1 | 1 | 1 | 1 × 1 = 1 |
[1, 2] | 1: 1, 2: 1 | 1 | 1 | 1 × 1 = 1 |
Thus, the sum of weights is 1 + 1 + 1 = 3
.
Example 3:
Input: nums = [4,3,4,3], k = 3
Output: 14
Explanation:
Subarrays of length k = 3
are:
Subarray | Frequencies | Mode | Mode Frequency |
Weight |
---|---|---|---|---|
[4, 3, 4] | 4: 2, 3: 1 | 4 | 2 | 2 × 4 = 8 |
[3, 4, 3] | 3: 2, 4: 1 | 3 | 2 | 2 × 3 = 6 |
Thus, the sum of weights is 8 + 6 = 14
.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
1 <= k <= nums.length
Solutions
Solution 1: Hash Map + Priority Queue + Sliding Window + Lazy Deletion
We use a hash map \(\textit{cnt}\) to record the frequency of each number in the current window. We use a priority queue \(\textit{pq}\) to store the frequency and value of each number in the current window, with priority given to higher frequency, and for equal frequency, to smaller numbers.
We design a function \(\textit{get_mode()}\) to obtain the mode and its frequency in the current window. Specifically, we repeatedly pop the top element from the priority queue until its frequency matches the frequency recorded in the hash map; at that point, the top element is the mode and its frequency for the current window.
We use a variable \(\textit{ans}\) to record the sum of weights for all windows. Initially, we add the first \(k\) numbers of the array to the hash map and priority queue, then call \(\textit{get_mode()}\) to get the mode and its frequency for the first window, and add its weight to \(\textit{ans}\).
Next, starting from the \(k\)-th number, we add each number to the hash map and priority queue, and decrement the frequency of the leftmost number in the window in the hash map. Then, we call \(\textit{get_mode()}\) to get the mode and its frequency for the current window, and add its weight to \(\textit{ans}\).
Finally, we return \(\textit{ans}\).
The time complexity is \(O(n \log k)\), and the space complexity is \(O(k)\).
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 |
|
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 |
|
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 |
|
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 |
|