Skip to content

Commit a916632

Browse files
committed
Add solution #1679
1 parent 920ef1d commit a916632

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
1668|[Maximum Repeating Substring](./1668-maximum-repeating-substring.js)|Easy|
352352
1669|[Merge In Between Linked Lists](./1669-merge-in-between-linked-lists.js)|Medium|
353353
1672|[Richest Customer Wealth](./1672-richest-customer-wealth.js)|Easy|
354+
1679|[Max Number of K-Sum Pairs](./1679-max-number-of-k-sum-pairs.js)|Medium|
354355
1716|[Calculate Money in Leetcode Bank](./1716-calculate-money-in-leetcode-bank.js)|Easy|
355356
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
356357
1768|[Merge Strings Alternately](./1768-merge-strings-alternately.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1679. Max Number of K-Sum Pairs
3+
* https://leetcode.com/problems/max-number-of-k-sum-pairs/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums and an integer k.
7+
*
8+
* In one operation, you can pick two numbers from the array whose sum equals k and
9+
* remove them from the array.
10+
*
11+
* Return the maximum number of operations you can perform on the array.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var maxOperations = function(nums, k) {
20+
const map = new Map();
21+
let result = 0;
22+
23+
nums.forEach(n => {
24+
const diff = k - n;
25+
26+
if (map.get(diff)) {
27+
result++;
28+
map.set(diff, map.get(diff) - 1);
29+
} else {
30+
map.set(n, (map.get(n) ?? 0) + 1);
31+
}
32+
});
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)