We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 334d551 commit f4e4925Copy full SHA for f4e4925
problems/problems_3318/solution.py
@@ -7,5 +7,19 @@ def solve(self, test_input=None):
7
return self.findXSum(*test_input)
8
9
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
10
- pass
11
-
+ ans = []
+ counter = Counter()
12
+ length = 0
13
+ for i, num in enumerate(nums):
14
+ counter[num] += 1
15
+ length += 1
16
+ if length == k:
17
+ most_common = sorted(counter.items(), key=lambda item: (-item[1], -item[0]))
18
+ top_x = [item[0] for item in most_common]
19
+ tmp = 0
20
+ for j in range(min(len(top_x), x)):
21
+ tmp += top_x[j] * counter[top_x[j]]
22
+ ans.append(tmp)
23
+ counter[nums[i - k + 1]] -= 1
24
+ length -= 1
25
+ return ans
0 commit comments