Skip to content

Commit d81692b

Browse files
committed
Sync LeetCode submission Runtime - 417 ms (57.89%), Memory - 44.3 MB (92.98%)
1 parent fe251f7 commit d81692b

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> consisting of <code>3 * n</code> elements.</p>
2+
3+
<p>You are allowed to remove any <strong>subsequence</strong> of elements of size <strong>exactly</strong> <code>n</code> from <code>nums</code>. The remaining <code>2 * n</code> elements will be divided into two <strong>equal</strong> parts:</p>
4+
5+
<ul>
6+
<li>The first <code>n</code> elements belonging to the first part and their sum is <code>sum<sub>first</sub></code>.</li>
7+
<li>The next <code>n</code> elements belonging to the second part and their sum is <code>sum<sub>second</sub></code>.</li>
8+
</ul>
9+
10+
<p>The <strong>difference in sums</strong> of the two parts is denoted as <code>sum<sub>first</sub> - sum<sub>second</sub></code>.</p>
11+
12+
<ul>
13+
<li>For example, if <code>sum<sub>first</sub> = 3</code> and <code>sum<sub>second</sub> = 2</code>, their difference is <code>1</code>.</li>
14+
<li>Similarly, if <code>sum<sub>first</sub> = 2</code> and <code>sum<sub>second</sub> = 3</code>, their difference is <code>-1</code>.</li>
15+
</ul>
16+
17+
<p>Return <em>the <strong>minimum difference</strong> possible between the sums of the two parts after the removal of </em><code>n</code><em> elements</em>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [3,1,2]
24+
<strong>Output:</strong> -1
25+
<strong>Explanation:</strong> Here, nums has 3 elements, so n = 1.
26+
Thus we have to remove 1 element from nums and divide the array into two equal parts.
27+
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
28+
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
29+
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
30+
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [7,9,5,8,1,3]
37+
<strong>Output:</strong> 1
38+
<strong>Explanation:</strong> Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
39+
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
40+
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
41+
It can be shown that it is not possible to obtain a difference smaller than 1.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>nums.length == 3 * 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>5</sup></code></li>
51+
</ul>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def minimumDifference(self, nums: List[int]) -> int:
3+
n3, n = len(nums), len(nums) // 3
4+
5+
part1 = [0] * (n + 1)
6+
# max heap
7+
total = sum(nums[:n])
8+
ql = [-x for x in nums[:n]]
9+
heapq.heapify(ql)
10+
part1[0] = total
11+
12+
for i in range(n, n * 2):
13+
total += nums[i]
14+
heapq.heappush(ql, -nums[i])
15+
total -= -heapq.heappop(ql)
16+
part1[i - (n - 1)] = total
17+
18+
# min heap
19+
part2 = sum(nums[n * 2 :])
20+
qr = nums[n * 2 :]
21+
heapq.heapify(qr)
22+
ans = part1[n] - part2
23+
24+
for i in range(n * 2 - 1, n - 1, -1):
25+
part2 += nums[i]
26+
heapq.heappush(qr, nums[i])
27+
part2 -= heapq.heappop(qr)
28+
ans = min(ans, part1[i - n] - part2)
29+
30+
return ans

0 commit comments

Comments
 (0)