Skip to content

Commit 8dd45b5

Browse files
committed
[LeetCode Sync] Runtime - 2986 ms (76.19%), Memory - 43.7 MB (95.24%)
1 parent dc6e1e9 commit 8dd45b5

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>You are given an array <code>nums</code> of <code>n</code> integers and two integers <code>k</code> and <code>x</code>.</p>
2+
3+
<p>The <strong>x-sum</strong> of an array is calculated by the following procedure:</p>
4+
5+
<ul>
6+
<li>Count the occurrences of all elements in the array.</li>
7+
<li>Keep only the occurrences of the top <code>x</code> most frequent elements. If two elements have the same number of occurrences, the element with the <strong>bigger</strong> value is considered more frequent.</li>
8+
<li>Calculate the sum of the resulting array.</li>
9+
</ul>
10+
11+
<p><strong>Note</strong> that if an array has less than <code>x</code> distinct elements, its <strong>x-sum</strong> is the sum of the array.</p>
12+
13+
<p>Return an integer array <code>answer</code> of length <code>n - k + 1</code> where <code>answer[i]</code> is the <strong>x-sum</strong> of the <span data-keyword="subarray-nonempty">subarray</span> <code>nums[i..i + k - 1]</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,2,3,4,2,3], k = 6, x = 2</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">[6,10,12]</span></p>
22+
23+
<p><strong>Explanation:</strong></p>
24+
25+
<ul>
26+
<li>For subarray <code>[1, 1, 2, 2, 3, 4]</code>, only elements 1 and 2 will be kept in the resulting array. Hence, <code>answer[0] = 1 + 1 + 2 + 2</code>.</li>
27+
<li>For subarray <code>[1, 2, 2, 3, 4, 2]</code>, only elements 2 and 4 will be kept in the resulting array. Hence, <code>answer[1] = 2 + 2 + 2 + 4</code>. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.</li>
28+
<li>For subarray <code>[2, 2, 3, 4, 2, 3]</code>, only elements 2 and 3 are kept in the resulting array. Hence, <code>answer[2] = 2 + 2 + 2 + 3 + 3</code>.</li>
29+
</ul>
30+
</div>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">nums = [3,8,7,8,7,5], k = 2, x = 2</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">[11,15,15,15,12]</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p>Since <code>k == x</code>, <code>answer[i]</code> is equal to the sum of the subarray <code>nums[i..i + k - 1]</code>.</p>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>nums.length == n</code></li>
49+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
51+
<li><code>1 &lt;= x &lt;= k &lt;= nums.length</code></li>
52+
</ul>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution:
2+
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
3+
left = SortedList()
4+
right = SortedList()
5+
count = Counter()
6+
sum_val = 0
7+
n = len(nums)
8+
result = [0] * (n - k + 1)
9+
10+
def add(val: int):
11+
if count[val] == 0:
12+
return
13+
temp = (count[val], val)
14+
if left and temp > left[0]:
15+
nonlocal sum_val
16+
sum_val += temp[0] * temp[1]
17+
left.add(temp)
18+
else:
19+
right.add(temp)
20+
21+
def remove(val: int):
22+
if count[val] == 0:
23+
return
24+
temp = (count[val], val)
25+
if temp in left:
26+
nonlocal sum_val
27+
sum_val -= temp[0] * temp[1]
28+
left.remove(temp)
29+
else:
30+
right.remove(temp)
31+
32+
for i, val in enumerate(nums):
33+
remove(val)
34+
count[val] += 1
35+
add(val)
36+
j = i - k + 1
37+
if j < 0:
38+
continue
39+
while right and len(left) < x:
40+
temp = right.pop()
41+
left.add(temp)
42+
sum_val += temp[0] * temp[1]
43+
44+
while len(left) > x:
45+
temp = left.pop(0)
46+
sum_val -= temp[0] * temp[1]
47+
right.add(temp)
48+
result[j] = sum_val
49+
50+
remove(nums[j])
51+
count[nums[j]] -= 1
52+
add(nums[j])
53+
54+
return result

0 commit comments

Comments
 (0)