Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bcadad6

Browse files
committedMar 2, 2025
Add solution #2570
1 parent ccba755 commit bcadad6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@
681681
2529|[Maximum Count of Positive Integer and Negative Integer](./2529-maximum-count-of-positive-integer-and-negative-integer.js)|Easy|
682682
2535|[Difference Between Element Sum and Digit Sum of an Array](./2535-difference-between-element-sum-and-digit-sum-of-an-array.js)|Easy|
683683
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|
684685
2618|[Check if Object Instance of Class](./2618-check-if-object-instance-of-class.js)|Medium|
685686
2619|[Array Prototype Last](./2619-array-prototype-last.js)|Easy|
686687
2620|[Counter](./2620-counter.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.