{"id":9960,"date":"2023-02-18T22:01:11","date_gmt":"2023-02-19T06:01:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9960"},"modified":"2023-02-18T22:01:54","modified_gmt":"2023-02-19T06:01:54","slug":"2570-merge-two-2d-arrays-by-summing-values","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/2570-merge-two-2d-arrays-by-summing-values\/","title":{"rendered":"\u82b1\u82b1\u9171 2570. Merge Two 2D Arrays by Summing Values"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>2D<\/strong>&nbsp;integer arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2.<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums1[i] = [id<sub>i<\/sub>, val<sub>i<\/sub>]<\/code>&nbsp;indicate that the number with the id&nbsp;<code>id<sub>i<\/sub><\/code>&nbsp;has a value equal to&nbsp;<code>val<sub>i<\/sub><\/code>.<\/li><li><code>nums2[i] = [id<sub>i<\/sub>, val<sub>i<\/sub>]<\/code>&nbsp;indicate that the number with the id&nbsp;<code>id<sub>i<\/sub><\/code>&nbsp;has a value equal to&nbsp;<code>val<sub>i<\/sub><\/code>.<\/li><\/ul>\n\n\n\n<p>Each array contains&nbsp;<strong>unique<\/strong>&nbsp;ids and is sorted in&nbsp;<strong>ascending<\/strong>&nbsp;order by id.<\/p>\n\n\n\n<p>Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Only ids that appear in at least one of the two arrays should be included in the resulting array.<\/li><li>Each id should be included&nbsp;<strong>only once<\/strong>&nbsp;and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays then its value in that array is considered to be&nbsp;<code>0<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the resulting array<\/em>. The returned array must be sorted in ascending order by id.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]\n<strong>Output:<\/strong> [[1,6],[2,3],[3,2],[4,6]]\n<strong>Explanation:<\/strong> The resulting array contains the following:\n- id = 1, the value of this id is 2 + 4 = 6.\n- id = 2, the value of this id is 3.\n- id = 3, the value of this id is 2.\n- id = 4, the value of this id is 5 + 1 = 6.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]\n<strong>Output:<\/strong> [[1,3],[2,4],[3,6],[4,3],[5,5]]\n<strong>Explanation:<\/strong> There are no common ids, so we just include each id with its value in the resulting list.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length, nums2.length &lt;= 200<\/code><\/li><li><code>nums1[i].length == nums2[j].length == 2<\/code><\/li><li><code>1 &lt;= id<sub>i<\/sub>, val<sub>i<\/sub> &lt;= 1000<\/code><\/li><li>Both arrays contain unique ids.<\/li><li>Both arrays are in&nbsp;strictly ascending order by id.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n    vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) {\n      vector<vector<int>> ans;\n      for (int i = 0, j = 0; i < nums1.size() || j < nums2.size();) {\n        const int id1 = i < nums1.size() ? nums1[i][0] : INT_MAX;\n        const int id2 = j < nums2.size() ? nums2[j][0] : INT_MAX;\n        if (id1 == id2) {\n          ans.push_back({id1, nums1[i++][1] + nums2[j++][1]});\n        } else if (id1 < id2) {\n          ans.push_back(nums1[i++]);\n        } else {\n          ans.push_back(nums2[j++]);\n        }\n      }\n      return ans;\n    }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;2D&nbsp;integer arrays&nbsp;nums1&nbsp;and&nbsp;nums2. nums1[i] = [idi, vali]&nbsp;indicate that the number with the id&nbsp;idi&nbsp;has a value equal to&nbsp;vali. nums2[i] = [idi, vali]&nbsp;indicate that the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[222,175],"class_list":["post-9960","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-easy","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9960"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9960\/revisions"}],"predecessor-version":[{"id":9962,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9960\/revisions\/9962"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}