Skip to content

Commit 7eb3784

Browse files
committed
88: Merge Sorted Array
1 parent bfdeba0 commit 7eb3784

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

Problems/88.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
//
2+
// Created by Amit Kumar on 24/09/2023
3+
//
4+
// https://leetcode.com/problems/merge-sorted-array
5+
//
6+
7+
#include "vector"
8+
9+
using namespace std;
110
class Solution {
211
public:
312
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
4-
vector<int>ans(m+n);
5-
int c = 0,l=0 , r=0;
6-
while (l<m and r <n) {
7-
if (nums1[l] < nums2[r]) {
8-
ans[c++] = nums1[l++];
13+
int i = m - 1;
14+
int j = n - 1;
15+
int k = m + n - 1;
16+
17+
while (j >= 0) {
18+
if (i >= 0 and nums1.at(i) >= nums2.at(j)) {
19+
nums1.at(k) = nums1.at(i);
20+
i--;
921
} else {
10-
ans[c++] = nums2[r++];
22+
nums1.at(k) = nums2.at(j);
23+
j--;
1124
}
25+
k--;
1226
}
13-
while (l<m) {
14-
ans[c++] = nums1[l++];
15-
}
16-
while (r<n) {
17-
ans[c++] = nums2[r++];
18-
}
19-
nums1 = ans;
2027
}
21-
};
28+
};

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# LeetCode
2-
Solution of Some LeetCode Problems
2+
3+
4+
| SOLID Design Pattern | YouTube Video Link | Code File |
5+
|----------------------|-------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|
6+
| 88 | [Leetcode Question 88: Merge Sorted Array, Top Interview 150 Series , C++ Live Coding Tutorial](https://youtu.be/wnt8lkFH4vY) | [88.cpp](Problems/88.cpp/) |

0 commit comments

Comments
 (0)