Skip to content

Commit 4200d98

Browse files
committed
Time: 24 ms (62.86%), Space: 18 MB (15.32%) - LeetHub
1 parent b737a04 commit 4200d98

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# time complexity: O(n^2 * klogk)
2+
# space complexity: O(n)
3+
from typing import Counter, List
4+
5+
6+
class Solution:
7+
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
8+
n = len(nums)
9+
result = []
10+
11+
for i in range(n - k + 1):
12+
items = list(Counter(nums[i:i+k]).items())
13+
items.sort(key=lambda item: (item[1], item[0]), reverse=True)
14+
15+
tempSum = 0
16+
for j in range(min(x, len(items))):
17+
tempSum += items[j][0] * items[j][1]
18+
19+
result.append(tempSum)
20+
21+
return result
22+
23+
24+
nums = [1, 1, 2, 2, 3, 4, 2, 3]
25+
k = 6
26+
x = 2
27+
print(Solution().findXSum(nums, k, x))
28+
nums = [3, 8, 7, 8, 7, 5]
29+
k = 2
30+
x = 2
31+
print(Solution().findXSum(nums, k, x))
32+
nums = [9, 2, 2]
33+
k = 3
34+
x = 3
35+
print(Solution().findXSum(nums, k, x))

0 commit comments

Comments
 (0)