Skip to content

Commit 7b990c6

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (62.54%)
1 parent 52d9685 commit 7b990c6

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given two <strong>2D</strong> integer arrays <code>nums1</code> and <code>nums2.</code></p>
2+
3+
<ul>
4+
<li><code>nums1[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li>
5+
<li><code>nums2[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li>
6+
</ul>
7+
8+
<p>Each array contains <strong>unique</strong> ids and is sorted in <strong>ascending</strong> order by id.</p>
9+
10+
<p>Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:</p>
11+
12+
<ul>
13+
<li>Only ids that appear in at least one of the two arrays should be included in the resulting array.</li>
14+
<li>Each id should be included <strong>only once</strong> 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 assume its value in that array to be <code>0</code>.</li>
15+
</ul>
16+
17+
<p>Return <em>the resulting array</em>. The returned array must be sorted in ascending order by id.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
24+
<strong>Output:</strong> [[1,6],[2,3],[3,2],[4,6]]
25+
<strong>Explanation:</strong> The resulting array contains the following:
26+
- id = 1, the value of this id is 2 + 4 = 6.
27+
- id = 2, the value of this id is 3.
28+
- id = 3, the value of this id is 2.
29+
- id = 4, the value of this id is 5 + 1 = 6.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
36+
<strong>Output:</strong> [[1,3],[2,4],[3,6],[4,3],[5,5]]
37+
<strong>Explanation:</strong> There are no common ids, so we just include each id with its value in the resulting list.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 200</code></li>
45+
<li><code>nums1[i].length == nums2[j].length == 2</code></li>
46+
<li><code>1 &lt;= id<sub>i</sub>, val<sub>i</sub> &lt;= 1000</code></li>
47+
<li>Both arrays contain unique ids.</li>
48+
<li>Both arrays are in&nbsp;strictly ascending order by id.</li>
49+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach: Two Pointers
2+
3+
# Time: O(n1 + n2)
4+
# Space: O(n1 + n2)
5+
6+
class Solution:
7+
def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
8+
n1, n2 = len(nums1), len(nums2)
9+
p1, p2 = 0, 0
10+
merged_array = []
11+
12+
while p1 < n1 and p2 < n2:
13+
if nums1[p1][0] == nums2[p2][0]:
14+
merged_array.append([nums1[p1][0], nums1[p1][1] + nums2[p2][1]])
15+
p1 += 1
16+
p2 += 1
17+
18+
elif nums1[p1][0] < nums2[p2][0]:
19+
merged_array.append(nums1[p1])
20+
p1 += 1
21+
22+
else:
23+
merged_array.append(nums2[p2])
24+
p2 += 1
25+
26+
while p1 < n1:
27+
merged_array.append(nums1[p1])
28+
p1 += 1
29+
30+
while p2 < n2:
31+
merged_array.append(nums2[p2])
32+
p2 += 1
33+
34+
return merged_array
35+

0 commit comments

Comments
 (0)