File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 351
351
1668|[ Maximum Repeating Substring] ( ./1668-maximum-repeating-substring.js ) |Easy|
352
352
1669|[ Merge In Between Linked Lists] ( ./1669-merge-in-between-linked-lists.js ) |Medium|
353
353
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|
354
355
1716|[ Calculate Money in Leetcode Bank] ( ./1716-calculate-money-in-leetcode-bank.js ) |Easy|
355
356
1748|[ Sum of Unique Elements] ( ./1748-sum-of-unique-elements.js ) |Easy|
356
357
1768|[ Merge Strings Alternately] ( ./1768-merge-strings-alternately.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments