Problem of The Day: Find X-Sum of All K-Long Subarrays I
Problem Statement
Brute Force [Accepted]
class Solution:
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
ans = []
N = len(nums)
for i in range(N - k + 1):
counter = Counter(nums[i:i+k])
min_heap = []
for num, count in counter.items():
heapq.heappush(min_heap, [count, num])
if len(min_heap) > x:
heapq.heappop(min_heap)
curr = 0
while min_heap:
count, num = heapq.heappop(min_heap)
curr += (num * count)
ans.append(curr)
return ans