File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 681
681
2529|[ Maximum Count of Positive Integer and Negative Integer] ( ./2529-maximum-count-of-positive-integer-and-negative-integer.js ) |Easy|
682
682
2535|[ Difference Between Element Sum and Digit Sum of an Array] ( ./2535-difference-between-element-sum-and-digit-sum-of-an-array.js ) |Easy|
683
683
2542|[ Maximum Subsequence Score] ( ./2542-maximum-subsequence-score.js ) |Medium|
684
+ 2570|[ Merge Two 2D Arrays by Summing Values] ( ./2570-merge-two-2d-arrays-by-summing-values.js ) |Easy|
684
685
2618|[ Check if Object Instance of Class] ( ./2618-check-if-object-instance-of-class.js ) |Medium|
685
686
2619|[ Array Prototype Last] ( ./2619-array-prototype-last.js ) |Easy|
686
687
2620|[ Counter] ( ./2620-counter.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2570. Merge Two 2D Arrays by Summing Values
3
+ * https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are given two 2D integer arrays nums1 and nums2.
7
+ * - nums1[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
8
+ * - nums2[i] = [idi, vali] indicate that the number with the id idi has a value equal to vali.
9
+ *
10
+ * Each array contains unique ids and is sorted in ascending order by id.
11
+ *
12
+ * Merge the two arrays into one array that is sorted in ascending order by id, respecting the
13
+ * following conditions:
14
+ * - Only ids that appear in at least one of the two arrays should be included in the resulting
15
+ * array.
16
+ * - Each id should be included only once and its value should be the sum of the values of this
17
+ * id in the two arrays. If the id does not exist in one of the two arrays, then assume its
18
+ * value in that array to be 0.
19
+ *
20
+ * Return the resulting array. The returned array must be sorted in ascending order by id.
21
+ */
22
+
23
+ /**
24
+ * @param {number[][] } nums1
25
+ * @param {number[][] } nums2
26
+ * @return {number[][] }
27
+ */
28
+ var mergeArrays = function ( nums1 , nums2 ) {
29
+ const map = new Map ( nums1 ) ;
30
+ for ( const [ key , value ] of nums2 ) {
31
+ map . set ( key , ( map . get ( key ) ?? 0 ) + value ) ;
32
+ }
33
+ return [ ...map ] . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments