diff --git a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/README.md b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/README.md index fe89109e8..84f078fcf 100755 --- a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/README.md +++ b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/README.md @@ -1,28 +1,32 @@ # [1679.Max Number of K-Sum Pairs][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 an integer array `nums` and an integer `k`. + +In one operation, you can pick two numbers from the array whose sum equals `k` and remove them from the array. + +Return the maximum number of operations you can perform on the array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,4], k = 5 +Output: 2 +Explanation: Starting with nums = [1,2,3,4]: +- Remove numbers 1 and 4, then nums = [2,3] +- Remove numbers 2 and 3, then nums = [] +There are no more pairs that sum up to 5, hence a total of 2 operations. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Max Number of K-Sum Pairs -```go ``` - +Input: nums = [3,1,3,4,3], k = 6 +Output: 1 +Explanation: Starting with nums = [3,1,3,4,3]: +- Remove the first two 3's, then nums = [1,4,3] +There are no more pairs that sum up to 6, hence a total of 1 operation. +``` ## 结语 diff --git a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution.go b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution.go index d115ccf5e..3abe9cb12 100644 --- a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution.go +++ b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution.go @@ -1,5 +1,23 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int, k int) int { + sort.Ints(nums) + s, e := 0, len(nums)-1 + ans := 0 + for s < e { + sum := nums[s] + nums[e] + if sum == k { + ans++ + s, e = s+1, e-1 + continue + } + if sum < k { + s++ + } else { + e-- + } + } + return ans } diff --git a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution_test.go b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution_test.go index 14ff50eb4..b21ab5560 100644 --- a/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution_test.go +++ b/leetcode/1601-1700/1679.Max-Number-of-K-Sum-Pairs/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3, 4}, 5, 2}, + {"TestCase2", []int{3, 1, 3, 4, 3}, 6, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }