Skip to content

Commit 778d7a4

Browse files
authored
Merge pull request #925 from 0xff-dev/1679
Add solution and test-cases for problem 1679
2 parents 506c749 + 988b4d3 commit 778d7a4

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
# [1679.Max Number of K-Sum Pairs][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 an integer array `nums` and an integer `k`.
5+
6+
In one operation, you can pick two numbers from the array whose sum equals `k` and remove them from the array.
7+
8+
Return the maximum number of operations you can perform on the array.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,3,4], k = 5
14+
Output: 2
15+
Explanation: Starting with nums = [1,2,3,4]:
16+
- Remove numbers 1 and 4, then nums = [2,3]
17+
- Remove numbers 2 and 3, then nums = []
18+
There are no more pairs that sum up to 5, hence a total of 2 operations.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Max Number of K-Sum Pairs
23-
```go
2423
```
25-
24+
Input: nums = [3,1,3,4,3], k = 6
25+
Output: 1
26+
Explanation: Starting with nums = [3,1,3,4,3]:
27+
- Remove the first two 3's, then nums = [1,4,3]
28+
There are no more pairs that sum up to 6, hence a total of 1 operation.
29+
```
2630

2731
## 结语
2832

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int) int {
6+
sort.Ints(nums)
7+
s, e := 0, len(nums)-1
8+
ans := 0
9+
for s < e {
10+
sum := nums[s] + nums[e]
11+
if sum == k {
12+
ans++
13+
s, e = s+1, e-1
14+
continue
15+
}
16+
if sum < k {
17+
s++
18+
} else {
19+
e--
20+
}
21+
}
22+
return ans
523
}

leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 4}, 5, 2},
18+
{"TestCase2", []int{3, 1, 3, 4, 3}, 6, 1},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)