Sum of Weighted Modes in Subarrays Problem
Description
LeetCode Problem 3672.
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
xis 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 <= 10^51 <= nums[i] <= 10^51 <= k <= nums.length
Solution
Approach: Sliding Window with a Heap and Lazy Deletion
Keep a hash map cnt with the frequency of each value in the current window, and a max-heap of (frequency, value) pairs ordered by higher frequency first and, on ties, smaller value first.
To read the mode of the window, look at the top of the heap. If its frequency still equals cnt[value], it is current: the mode is that value and the weight is frequency * value. Otherwise the entry is stale, so pop it and look again. This is lazy deletion: old entries are never removed when a frequency changes, only when they reach the top.
Fill the first window, add its weight, then slide: add the new element (increase its count, push the new pair), remove the oldest one (decrease its count, push the new pair), and add the weight of the new window. The sum can be large, so use a 64-bit integer.
Complexity: time O(n log n), since each slide pushes two entries into the heap; space O(n) for the heap in the worst case (the map itself holds at most k values).
from collections import defaultdict
from heapq import heappop, heappush
class Solution:
def modeWeight(self, nums: List[int], k: int) -> int:
pq = []
cnt = defaultdict(int)
for x in nums[:k]:
cnt[x] += 1
heappush(pq, (-cnt[x], x))
def get_mode() -> int:
while -pq[0][0] != cnt[pq[0][1]]:
heappop(pq)
freq, val = -pq[0][0], pq[0][1]
return freq * val
ans = 0
ans += get_mode()
for i in range(k, len(nums)):
x, y = nums[i], nums[i - k]
cnt[x] += 1
cnt[y] -= 1
heappush(pq, (-cnt[x], x))
heappush(pq, (-cnt[y], y))
ans += get_mode()
return ans