diff options
author | Jack Sangdahl <[email protected]> | 2025-03-01 19:28:17 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-01 19:28:17 -0700 |
commit | 4087d8f9090f84fe770bea6d621f3f24b912b40c (patch) | |
tree | 3bd97041c3f12381778722dcf2544d7a2d4e5846 /merge_two_2d_arr_sum.py | |
parent | d244407ce1f87bc1bd01cd3199cc179433582e70 (diff) | |
download | leetcode-4087d8f9090f84fe770bea6d621f3f24b912b40c.tar.gz leetcode-4087d8f9090f84fe770bea6d621f3f24b912b40c.zip |
2570. merge two 2d arrays by summing values
Diffstat (limited to 'merge_two_2d_arr_sum.py')
-rw-r--r-- | merge_two_2d_arr_sum.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/merge_two_2d_arr_sum.py b/merge_two_2d_arr_sum.py new file mode 100644 index 0000000..38b758b --- /dev/null +++ b/merge_two_2d_arr_sum.py @@ -0,0 +1,42 @@ +# 2570. Merge Two 2D Arrays by Summing Values + +""" +you are given two 2d integer arrays 'nums1' and 'nums2'. 'nums1[i] = [id_i, +val_i]' indicate that the number with id 'id_i' has a value equal. to +'val_i'. 'nums2[i] = [id_i, val_i]' indicates that the number with the id +'id_i' has a value equal to 'val_i'. each array contains unique ids and is +sorted in ascending order by id. merge the two arrays into one array that is +sorted in ascending order by id. return the resulting array. the returned +array must be sorted in ascending order by id. +""" + + +class Solution(object): + def mergeArrays(self, nums1, nums2): + """ + :type nums1: List[List[int]] + :type nums2: List[List[int]] + :rtype: List[List[int]] + """ + i, j, ans = 0, 0, [] + n1, n2 = len(nums1), len(nums2) + while i < n1 or j < n2: + if i < n1 and (j >= n2 or nums1[i][0] < nums2[j][0]): + ans.append(nums1[i]) + i += 1 + elif j < n2 and (i >= n1 or nums1[i][0] > nums2[j][0]): + ans.append(nums2[j]) + j += 1 + else: + ans.append([nums1[i][0], nums1[i][1] + nums2[j][1]]) + i += 1 + j += 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print( + obj.mergeArrays(nums1=[[1, 2], [2, 3], [4, 5]], nums2=[[1, 4], [3, 2], [4, 1]]) + ) + print(obj.mergeArrays(nums1=[[2, 4], [3, 6], [5, 5]], nums2=[[1, 3], [4, 3]])) |