-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 185 ms (57.14%), Space: 68.1 MB (37.46%) - LeetHub
- Loading branch information
1 parent
a6131bc
commit a20f8c6
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
1679-max-number-of-k-sum-pairs/1679-max-number-of-k-sum-pairs.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public: | ||
int maxOperations(vector<int>& nums, int k) { | ||
int count = 0; | ||
unordered_map<int, int> map; | ||
for (int i : nums) { | ||
int res = k - i; | ||
if (map.count(res)) { | ||
if (map[res] == 1) { | ||
map.erase(res); | ||
} else { | ||
map[res]--; | ||
} | ||
count++; | ||
} else { | ||
map[i]++; | ||
} | ||
} | ||
return count; | ||
} | ||
}; |