Skip to content

Commit 8d764b0

Browse files
authored
feat: add cpp solution to lc problem: No.1679
No.1679.Max Number of K-Sum Pairs/README.md
1 parent 399fa00 commit 8d764b0

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,29 @@ class Solution {
9696
}
9797
```
9898

99-
### **...**
100-
101-
```
99+
### **C++**
102100

101+
```cpp
102+
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++;
118+
}
119+
return cnt;
120+
}
121+
};
103122
```
104123
105124
<!-- tabs:end -->

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,29 @@ class Solution {
8686
}
8787
```
8888

89-
### **...**
90-
91-
```
89+
### **C++**
9290

91+
```cpp
92+
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++;
108+
}
109+
return cnt;
110+
}
111+
};
93112
```
94113
95114
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
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++;
17+
}
18+
return cnt;
19+
}
20+
};

0 commit comments

Comments
 (0)