Skip to content

Commit 08c4458

Browse files
authored
feat: add solutions to lc problem: No.1679 (doocs#725)
No.1679.Max Number of K-Sum Pairs
1 parent 4847be1 commit 08c4458

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,46 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57-
57+
class Solution:
58+
def maxOperations(self, nums: List[int], k: int) -> int:
59+
nums.sort()
60+
ans, l, r = 0, 0, len(nums) - 1
61+
while l < r:
62+
if nums[l] + nums[r] > k:
63+
r -= 1
64+
elif nums[l] + nums[r] < k:
65+
l += 1
66+
else:
67+
ans += 1
68+
l += 1
69+
r -= 1
70+
return ans
5871
```
5972

6073
### **Java**
6174

6275
<!-- 这里可写当前语言的特殊实现逻辑 -->
6376

6477
```java
65-
78+
class Solution {
79+
public int maxOperations(int[] nums, int k) {
80+
Arrays.sort(nums);
81+
int l = 0, r = nums.length - 1;
82+
int ans = 0;
83+
while (l < r) {
84+
if (nums[l] + nums[r] > k) {
85+
r--;
86+
} else if (nums[l] + nums[r] < k) {
87+
l++;
88+
} else {
89+
ans++;
90+
l++;
91+
r--;
92+
}
93+
}
94+
return ans;
95+
}
96+
}
6697
```
6798

6899
### **...**

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,44 @@ There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maxOperations(self, nums: List[int], k: int) -> int:
51+
nums.sort()
52+
ans, l, r = 0, 0, len(nums) - 1
53+
while l < r:
54+
if nums[l] + nums[r] > k:
55+
r -= 1
56+
elif nums[l] + nums[r] < k:
57+
l += 1
58+
else:
59+
ans += 1
60+
l += 1
61+
r -= 1
62+
return ans
5063
```
5164

5265
### **Java**
5366

5467
```java
55-
68+
class Solution {
69+
public int maxOperations(int[] nums, int k) {
70+
Arrays.sort(nums);
71+
int l = 0, r = nums.length - 1;
72+
int ans = 0;
73+
while (l < r) {
74+
if (nums[l] + nums[r] > k) {
75+
r--;
76+
} else if (nums[l] + nums[r] < k) {
77+
l++;
78+
} else {
79+
ans++;
80+
l++;
81+
r--;
82+
}
83+
}
84+
return ans;
85+
}
86+
}
5687
```
5788

5889
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maxOperations(int[] nums, int k) {
3+
Arrays.sort(nums);
4+
int l = 0, r = nums.length - 1;
5+
int ans = 0;
6+
while (l < r) {
7+
if (nums[l] + nums[r] > k) {
8+
r--;
9+
} else if (nums[l] + nums[r] < k) {
10+
l++;
11+
} else {
12+
ans++;
13+
l++;
14+
r--;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxOperations(self, nums: List[int], k: int) -> int:
3+
nums.sort()
4+
ans, l, r = 0, 0, len(nums) - 1
5+
while l < r:
6+
if nums[l] + nums[r] > k:
7+
r -= 1
8+
elif nums[l] + nums[r] < k:
9+
l += 1
10+
else:
11+
ans += 1
12+
l += 1
13+
r -= 1
14+
return ans

0 commit comments

Comments
 (0)