Skip to content

Add solution and test-cases for problem 2163 #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [2163.Minimum Difference in Sums After Removal of Elements][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a **0-indexed** integer array `nums` consisting of `3 * n` elements.

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:

- The first `n` elements belonging to the first part and their sum is `sumfirst`.
- The next `n` elements belonging to the second part and their sum is `sumsecond`.

The **difference in sums** of the two parts is denoted as `sumfirst - sumsecond`.

- For example, if `sumfirst = 3` and `sumsecond = 2`, their difference is `1`.
- Similarly, if `sumfirst = 2` and `sumsecond = 3`, their difference is `-1`.

Return the **minimum difference** possible between the sums of the two parts after the removal of `n` elements.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Minimum Difference in Sums After Removal of Elements
```go
```

Input: nums = [7,9,5,8,1,3]
Output: 1
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
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.
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.
It can be shown that it is not possible to obtain a difference smaller than 1.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
package Solution

func Solution(x bool) bool {
import "container/heap"

func Solution(nums []int) int64 {
n3 := len(nums)
n := n3 / 3
part1 := make([]int64, n+1)
var sum int64 = 0
ql := &MaxHeap{}
heap.Init(ql)
for i := 0; i < n; i++ {
sum += int64(nums[i])
heap.Push(ql, nums[i])
}
part1[0] = sum
for i := n; i < n*2; i++ {
sum += int64(nums[i])
heap.Push(ql, nums[i])
sum -= int64(heap.Pop(ql).(int))
part1[i-(n-1)] = sum
}

var part2 int64 = 0
qr := &IntMinHeap{}
heap.Init(qr)
for i := n*3 - 1; i >= n*2; i-- {
part2 += int64(nums[i])
heap.Push(qr, nums[i])
}
ans := part1[n] - part2
for i := n*2 - 1; i >= n; i-- {
part2 += int64(nums[i])
heap.Push(qr, nums[i])
part2 -= int64(heap.Pop(qr).(int))
if part1[i-n]-part2 < ans {
ans = part1[i-n] - part2
}
}
return ans
}

type MaxHeap []int

func (h MaxHeap) Len() int { return len(h) }
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *MaxHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}

type IntMinHeap []int

func (h IntMinHeap) Len() int { return len(h) }
func (h IntMinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntMinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntMinHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntMinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{3, 1, 2}, -1},
{"TestCase2", []int{7, 9, 5, 8, 1, 3}, 1},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading