Skip to content

Commit 6415c48

Browse files
authored
Update max-number-of-k-sum-pairs.cpp
1 parent adef43e commit 6415c48

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

C++/max-number-of-k-sum-pairs.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ class Solution {
55
public:
66
int maxOperations(vector<int>& nums, int k) {
77
unordered_map<int, int> lookup;
8-
for (const auto& num : nums) {
9-
++lookup[num];
10-
}
118
int result = 0;
12-
for (const auto& [num, cnt] : lookup) {
13-
if (k - num == num) {
14-
result += cnt / 2;
15-
} else if (lookup.count(k - num)) {
16-
int c = min(cnt, lookup[k - num]);
17-
result += c;
18-
lookup[num] -= c;
19-
lookup[k - num] -= c;
9+
for (const auto& num : nums) {
10+
if (lookup.count(k - num) && lookup[k - num]) {
11+
--lookup[k - num];
12+
++result;
13+
} else {
14+
++lookup[num];
2015
}
2116
}
2217
return result;

0 commit comments

Comments
 (0)