3610. Find X-Sum of All K-Long Subarrays I
My accepted Python solution to LeetCode problem 3610, Find X-Sum of All K-Long Subarrays I, running in 19ms.
- Difficulty: Easy
- Python
- Runtime 19ms
- Memory 17.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 19ms, memory 17.6MB, accepted 2026-01-01.
class Solution:
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
from collections import Counter
def get_x_sum(subarr):
freq = Counter(subarr)
# Sort by frequency desc, then by value desc
items = sorted(freq.items(), key=lambda p: (p[1], p[0]), reverse=True)
# Take top x elements
top_x = items[:x]
return sum(val * count for val, count in top_x)
result = []
for i in range(len(nums) - k + 1):
subarr = nums[i:i+k]
result.append(get_x_sum(subarr))
return result