Skip to content

Commit f4e4925

Browse files
committed
test: 3318 solution
py
1 parent 334d551 commit f4e4925

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

problems/problems_3318/solution.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,19 @@ def solve(self, test_input=None):
77
return self.findXSum(*test_input)
88

99
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
10-
pass
11-
10+
ans = []
11+
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

Comments
 (0)