Skip to content

Commit a1721c9

Browse files
authored
Merge pull request #1263 from 0xff-dev/2163
Add solution and test-cases for problem 2163
2 parents c7a29ac + d48e827 commit a1721c9

File tree

3 files changed

+100
-22
lines changed

3 files changed

+100
-22
lines changed

leetcode/2101-2200/2163.Minimum-Difference-in-Sums-After-Removal-of-Elements/README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [2163.Minimum Difference in Sums After Removal of Elements][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** integer array `nums` consisting of `3 * n` elements.
5+
6+
You are allowed to remove any **subsequence** of elements of size **exactly** `n` from `nums`. The remaining `2 * n` elements will be divided into two **equal** parts:
7+
8+
- The first `n` elements belonging to the first part and their sum is `sumfirst`.
9+
- The next `n` elements belonging to the second part and their sum is `sumsecond`.
10+
11+
The **difference in sums** of the two parts is denoted as `sumfirst - sumsecond`.
12+
13+
- For example, if `sumfirst = 3` and `sumsecond = 2`, their difference is `1`.
14+
- Similarly, if `sumfirst = 2` and `sumsecond = 3`, their difference is `-1`.
15+
16+
Return the **minimum difference** possible between the sums of the two parts after the removal of `n` elements.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: nums = [3,1,2]
22+
Output: -1
23+
Explanation: Here, nums has 3 elements, so n = 1.
24+
Thus we have to remove 1 element from nums and divide the array into two equal parts.
25+
- 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.
26+
- 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.
27+
- 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.
28+
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
1329
```
1430

15-
## 题意
16-
> ...
17-
18-
## 题解
31+
**Example 2:**
1932

20-
### 思路1
21-
> ...
22-
Minimum Difference in Sums After Removal of Elements
23-
```go
2433
```
25-
34+
Input: nums = [7,9,5,8,1,3]
35+
Output: 1
36+
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
37+
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.
38+
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.
39+
It can be shown that it is not possible to obtain a difference smaller than 1.
40+
```
2641

2742
## 结语
2843

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
func Solution(nums []int) int64 {
6+
n3 := len(nums)
7+
n := n3 / 3
8+
part1 := make([]int64, n+1)
9+
var sum int64 = 0
10+
ql := &MaxHeap{}
11+
heap.Init(ql)
12+
for i := 0; i < n; i++ {
13+
sum += int64(nums[i])
14+
heap.Push(ql, nums[i])
15+
}
16+
part1[0] = sum
17+
for i := n; i < n*2; i++ {
18+
sum += int64(nums[i])
19+
heap.Push(ql, nums[i])
20+
sum -= int64(heap.Pop(ql).(int))
21+
part1[i-(n-1)] = sum
22+
}
23+
24+
var part2 int64 = 0
25+
qr := &IntMinHeap{}
26+
heap.Init(qr)
27+
for i := n*3 - 1; i >= n*2; i-- {
28+
part2 += int64(nums[i])
29+
heap.Push(qr, nums[i])
30+
}
31+
ans := part1[n] - part2
32+
for i := n*2 - 1; i >= n; i-- {
33+
part2 += int64(nums[i])
34+
heap.Push(qr, nums[i])
35+
part2 -= int64(heap.Pop(qr).(int))
36+
if part1[i-n]-part2 < ans {
37+
ans = part1[i-n] - part2
38+
}
39+
}
40+
return ans
41+
}
42+
43+
type MaxHeap []int
44+
45+
func (h MaxHeap) Len() int { return len(h) }
46+
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
47+
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
48+
func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
49+
func (h *MaxHeap) Pop() interface{} {
50+
old := *h
51+
n := len(old)
52+
x := old[n-1]
53+
*h = old[0 : n-1]
54+
return x
55+
}
56+
57+
type IntMinHeap []int
58+
59+
func (h IntMinHeap) Len() int { return len(h) }
60+
func (h IntMinHeap) Less(i, j int) bool { return h[i] < h[j] }
61+
func (h IntMinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
62+
func (h *IntMinHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
63+
func (h *IntMinHeap) Pop() interface{} {
64+
old := *h
65+
n := len(old)
66+
x := old[n-1]
67+
*h = old[0 : n-1]
468
return x
569
}

leetcode/2101-2200/2163.Minimum-Difference-in-Sums-After-Removal-of-Elements/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 1, 2}, -1},
17+
{"TestCase2", []int{7, 9, 5, 8, 1, 3}, 1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)