Skip to content

Commit 3614710

Browse files
committed
feat: add solutions to lc problem: No.1679
No.1679.Max Number of K-Sum Pairs
1 parent 1139b6e commit 3614710

File tree

6 files changed

+180
-91
lines changed

6 files changed

+180
-91
lines changed

solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md

+72-30
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:排序**
51+
52+
对 nums 进行排序。然后 l, r 分别指向 nums 首尾元素,判断两整数之和 s 与 k 的大小关系。
53+
54+
-`s == k`,说明找到了两个整数,满足和为 k,答案 ans 加 1,然后 l, r 向中间移动;
55+
-`s > k`,则 r 指针向左移动;
56+
-`s > k`,则 l 指针向右移动;
57+
- 继续循环判断,直至 l >= r。
58+
59+
循环结束,返回 ans。
60+
61+
时间复杂度 O(nlogn),其中 n 为 nums 的长度。
62+
5063
<!-- tabs:start -->
5164

5265
### **Python3**
@@ -57,16 +70,16 @@
5770
class Solution:
5871
def maxOperations(self, nums: List[int], k: int) -> int:
5972
nums.sort()
60-
ans, l, r = 0, 0, len(nums) - 1
73+
l, r, ans = 0, len(nums) - 1, 0
6174
while l < r:
62-
if nums[l] + nums[r] > k:
75+
s = nums[l] + nums[r]
76+
if s == k:
77+
ans += 1
78+
l, r = l + 1, r - 1
79+
elif s > k:
6380
r -= 1
64-
elif nums[l] + nums[r] < k:
65-
l += 1
6681
else:
67-
ans += 1
6882
l += 1
69-
r -= 1
7083
return ans
7184
```
7285

@@ -81,14 +94,15 @@ class Solution {
8194
int l = 0, r = nums.length - 1;
8295
int ans = 0;
8396
while (l < r) {
84-
if (nums[l] + nums[r] > k) {
85-
r--;
86-
} else if (nums[l] + nums[r] < k) {
87-
l++;
97+
int s = nums[l] + nums[r];
98+
if (s == k) {
99+
++ans;
100+
++l;
101+
--r;
102+
} else if (s > k) {
103+
--r;
88104
} else {
89-
ans++;
90-
l++;
91-
r--;
105+
++l;
92106
}
93107
}
94108
return ans;
@@ -100,25 +114,53 @@ class Solution {
100114

101115
```cpp
102116
class Solution {
103-
public:
104-
int maxOperations(vector<int>& nums, int k) {
105-
int n = nums.size();
106-
sort(nums.begin(), nums.end());
107-
int cnt = 0;
108-
int i = 0, j = n - 1;
109-
while (i < j) {
110-
if (nums[i] + nums[j] == k) {
111-
i++;
112-
j--;
113-
cnt++;
114-
} else if (nums[i] + nums[j] > k) {
115-
j--;
116-
} else
117-
i++;
117+
public:
118+
int maxOperations(vector<int>& nums, int k) {
119+
sort(nums.begin(), nums.end());
120+
int cnt = 0;
121+
int i = 0, j = nums.size() - 1;
122+
while (i < j) {
123+
if (nums[i] + nums[j] == k) {
124+
i++;
125+
j--;
126+
cnt++;
127+
} else if (nums[i] + nums[j] > k) {
128+
j--;
129+
} else {
130+
i++;
131+
}
132+
}
133+
return cnt;
118134
}
119-
return cnt;
120-
}
121135
};
122136
```
123137
138+
### **Go**
139+
140+
```go
141+
func maxOperations(nums []int, k int) int {
142+
sort.Ints(nums)
143+
l, r, ans := 0, len(nums)-1, 0
144+
for l < r {
145+
s := nums[l] + nums[r]
146+
if s == k {
147+
ans++
148+
l++
149+
r--
150+
} else if s > k {
151+
r--
152+
} else {
153+
l++
154+
}
155+
}
156+
return ans
157+
}
158+
```
159+
160+
### **...**
161+
162+
```
163+
164+
```
165+
124166
<!-- tabs:end -->

solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md

+59-30
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
4949
class Solution:
5050
def maxOperations(self, nums: List[int], k: int) -> int:
5151
nums.sort()
52-
ans, l, r = 0, 0, len(nums) - 1
52+
l, r, ans = 0, len(nums) - 1, 0
5353
while l < r:
54-
if nums[l] + nums[r] > k:
54+
s = nums[l] + nums[r]
55+
if s == k:
56+
ans += 1
57+
l, r = l + 1, r - 1
58+
elif s > k:
5559
r -= 1
56-
elif nums[l] + nums[r] < k:
57-
l += 1
5860
else:
59-
ans += 1
6061
l += 1
61-
r -= 1
6262
return ans
6363
```
6464

@@ -71,14 +71,15 @@ class Solution {
7171
int l = 0, r = nums.length - 1;
7272
int ans = 0;
7373
while (l < r) {
74-
if (nums[l] + nums[r] > k) {
75-
r--;
76-
} else if (nums[l] + nums[r] < k) {
77-
l++;
74+
int s = nums[l] + nums[r];
75+
if (s == k) {
76+
++ans;
77+
++l;
78+
--r;
79+
} else if (s > k) {
80+
--r;
7881
} else {
79-
ans++;
80-
l++;
81-
r--;
82+
++l;
8283
}
8384
}
8485
return ans;
@@ -90,25 +91,53 @@ class Solution {
9091

9192
```cpp
9293
class Solution {
93-
public:
94-
int maxOperations(vector<int>& nums, int k) {
95-
int n = nums.size();
96-
sort(nums.begin(), nums.end());
97-
int cnt = 0;
98-
int i = 0, j = n - 1;
99-
while (i < j) {
100-
if (nums[i] + nums[j] == k) {
101-
i++;
102-
j--;
103-
cnt++;
104-
} else if (nums[i] + nums[j] > k) {
105-
j--;
106-
} else
107-
i++;
94+
public:
95+
int maxOperations(vector<int>& nums, int k) {
96+
sort(nums.begin(), nums.end());
97+
int cnt = 0;
98+
int i = 0, j = nums.size() - 1;
99+
while (i < j) {
100+
if (nums[i] + nums[j] == k) {
101+
i++;
102+
j--;
103+
cnt++;
104+
} else if (nums[i] + nums[j] > k) {
105+
j--;
106+
} else {
107+
i++;
108+
}
109+
}
110+
return cnt;
108111
}
109-
return cnt;
110-
}
111112
};
112113
```
113114
115+
### **Go**
116+
117+
```go
118+
func maxOperations(nums []int, k int) int {
119+
sort.Ints(nums)
120+
l, r, ans := 0, len(nums)-1, 0
121+
for l < r {
122+
s := nums[l] + nums[r]
123+
if s == k {
124+
ans++
125+
l++
126+
r--
127+
} else if s > k {
128+
r--
129+
} else {
130+
l++
131+
}
132+
}
133+
return ans
134+
}
135+
```
136+
137+
### **...**
138+
139+
```
140+
141+
```
142+
114143
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
2-
public:
3-
int maxOperations(vector<int>& nums, int k) {
4-
int n = nums.size();
5-
sort(nums.begin(), nums.end());
6-
int cnt = 0;
7-
int i = 0, j = n - 1;
8-
while (i < j) {
9-
if (nums[i] + nums[j] == k) {
10-
i++;
11-
j--;
12-
cnt++;
13-
} else if (nums[i] + nums[j] > k) {
14-
j--;
15-
} else
16-
i++;
2+
public:
3+
int maxOperations(vector<int>& nums, int k) {
4+
sort(nums.begin(), nums.end());
5+
int cnt = 0;
6+
int i = 0, j = nums.size() - 1;
7+
while (i < j) {
8+
if (nums[i] + nums[j] == k) {
9+
i++;
10+
j--;
11+
cnt++;
12+
} else if (nums[i] + nums[j] > k) {
13+
j--;
14+
} else {
15+
i++;
16+
}
17+
}
18+
return cnt;
1719
}
18-
return cnt;
19-
}
20-
};
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maxOperations(nums []int, k int) int {
2+
sort.Ints(nums)
3+
l, r, ans := 0, len(nums)-1, 0
4+
for l < r {
5+
s := nums[l] + nums[r]
6+
if s == k {
7+
ans++
8+
l++
9+
r--
10+
} else if s > k {
11+
r--
12+
} else {
13+
l++
14+
}
15+
}
16+
return ans
17+
}

solution/1600-1699/1679.Max Number of K-Sum Pairs/Solution.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ public int maxOperations(int[] nums, int k) {
44
int l = 0, r = nums.length - 1;
55
int ans = 0;
66
while (l < r) {
7-
if (nums[l] + nums[r] > k) {
8-
r--;
9-
} else if (nums[l] + nums[r] < k) {
10-
l++;
7+
int s = nums[l] + nums[r];
8+
if (s == k) {
9+
++ans;
10+
++l;
11+
--r;
12+
} else if (s > k) {
13+
--r;
1114
} else {
12-
ans++;
13-
l++;
14-
r--;
15+
++l;
1516
}
1617
}
1718
return ans;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution:
22
def maxOperations(self, nums: List[int], k: int) -> int:
33
nums.sort()
4-
ans, l, r = 0, 0, len(nums) - 1
4+
l, r, ans = 0, len(nums) - 1, 0
55
while l < r:
6-
if nums[l] + nums[r] > k:
6+
s = nums[l] + nums[r]
7+
if s == k:
8+
ans += 1
9+
l, r = l + 1, r - 1
10+
elif s > k:
711
r -= 1
8-
elif nums[l] + nums[r] < k:
9-
l += 1
1012
else:
11-
ans += 1
1213
l += 1
13-
r -= 1
1414
return ans

0 commit comments

Comments
 (0)