Skip to content

Commit f6590f7

Browse files
committed
day 11
1 parent f716e12 commit f6590f7

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

Leetcode Daily Challenge/January-2021/10. Create Sorted Array through Instructions.cpp

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Merge Sorted Array
3+
==================
4+
5+
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
6+
7+
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.
8+
9+
Example 1:
10+
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
11+
Output: [1,2,2,3,5,6]
12+
13+
Example 2:
14+
Input: nums1 = [1], m = 1, nums2 = [], n = 0
15+
Output: [1]
16+
17+
Constraints:
18+
0 <= n, m <= 200
19+
1 <= n + m <= 200
20+
nums1.length == m + n
21+
nums2.length == n
22+
-109 <= nums1[i], nums2[i] <= 109
23+
24+
Hint #1
25+
You can easily solve this problem if you simply think about two elements at a time rather than two arrays. We know that each of the individual arrays is sorted. What we don't know is how they will intertwine. Can we take a local decision and arrive at an optimal solution?
26+
27+
Hint #2
28+
If you simply consider one element each at a time from the two arrays and make a decision and proceed accordingly, you will arrive at the optimal solution.
29+
*/
30+
31+
class Solution
32+
{
33+
public:
34+
void merge(vector<int> &A, int m, vector<int> &B, int n)
35+
{
36+
int a = m - 1, b = n - 1;
37+
int i = m + n - 1;
38+
39+
while (a >= 0 && b >= 0)
40+
{
41+
if (A[a] > B[b])
42+
A[i--] = A[a--];
43+
else
44+
A[i--] = B[b--];
45+
}
46+
47+
while (b >= 0)
48+
A[i--] = B[b--];
49+
}
50+
};

Leetcode Daily Challenge/January-2021/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
| 7. | [Longest Substring Without Repeating Characters](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/579/week-1-january-1st-january-7th/3595/) | [cpp](./07.%20Longest%20Substring%20Without%20Repeating%20Characters.cpp) |
1212
| 8. | [Check If Two String Arrays are Equivalent](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3597/) | [cpp](./08.%20Check%20If%20Two%20String%20Arrays%20are%20Equivalent.cpp) |
1313
| 9. | [Word Ladder](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3598/) | [cpp](./09.%20Word%20Ladder.cpp) |
14-
| . | []() | [cpp](./.%20.cpp) |
15-
| . | []() | [cpp](./.%20.cpp) |
14+
| 10. | [Create Sorted Array through Instructions](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3599/) | [cpp](./10.%20.cpp) |
15+
| 11. | [Merge Sorted Array](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3600/) | [cpp](./11.%20Merge%20Sorted%20Array.cpp) |
16+
| . | []() | [cpp](./.cpp) |
17+
| . | []() | [cpp](./.cpp) |
18+
| . | []() | [cpp](./.cpp) |
19+
| . | []() | [cpp](./.cpp) |

0 commit comments

Comments
 (0)