Skip to content

Commit 74d7005

Browse files
committed
Jul 18
1 parent d49fc1b commit 74d7005

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from heapq import heapify, heappushpop
2+
3+
4+
class Solution:
5+
def minimumDifference(self, nums: list[int]) -> int:
6+
n = len(nums) // 3
7+
# minimise first array of size n elements
8+
first = [0] * (n + 1)
9+
max_heap = [-num for num in nums[:n]]
10+
heapify(max_heap)
11+
total = -sum(max_heap)
12+
first[0] = total
13+
for i, num in enumerate(nums[n:n * 2], start=1):
14+
total += num
15+
total -= -heappushpop(max_heap, -num)
16+
first[i] = total
17+
18+
# maxmimise last array of size n elements
19+
min_heap = nums[n * 2:]
20+
heapify(min_heap)
21+
last = sum(min_heap)
22+
ans = first[n] - last
23+
for i, num in enumerate(reversed(nums[n:n * 2]), start=1):
24+
last += num
25+
last -= heappushpop(min_heap, num)
26+
ans = min(ans, first[n - i] - last)
27+
return ans
28+
29+
30+
def main():
31+
nums = [3, 1, 2]
32+
assert Solution().minimumDifference(nums) == -1
33+
34+
nums = [7, 9, 5, 8, 1, 3]
35+
assert Solution().minimumDifference(nums) == 1
36+
37+
38+
if __name__ == '__main__':
39+
main()

2025-07-July-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
| July 15 | [3136. Valid Word](https://leetcode.com/problems/valid-word/) | Easy | Solved |
2222
| July 16 | [3201. Find the Maximum Length of Valid Subsequence I](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-i/) | Medium | Solved |
2323
| July 17 | [3202. Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/) | Medium | Unsolved |
24-
| July 18 | []() | | |
24+
| July 18 | [2163. Minimum Difference in Sums After Removal of Elements](https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/) | Hard | Unsolved |
2525
| July 19 | []() | | |
2626
| July 20 | []() | | |
2727
| July 21 | []() | | |
@@ -42,4 +42,4 @@
4242
| --- | --- | --- | --- |
4343
| Easy | 5 | 5 | 0 |
4444
| Medium | 7 | 6 | 1 |
45-
| Hard | 5 | 1 | 4 |
45+
| Hard | 6 | 1 | 5 |

0 commit comments

Comments
 (0)