Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1562 from supremvanam/main
Browse files Browse the repository at this point in the history
Create: 88-Merge-Sorted-Array.java
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
2 parents 8954592 + 83f9143 commit 0a318a2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions java/88-Merge-Sorted-Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {

// Time: O(m+n) | Space: O(1)
public void merge(int[] nums1, int m, int[] nums2, int n) {
// Three pointer technique
int r1 = m-1;
int r2 = n-1;

for(int w = m+n-1; w >= 0; w--) {
if(r1 >= 0 && r2 >= 0) {
nums1[w] = nums1[r1] > nums2[r2] ? nums1[r1--] : nums2[r2--];
} else if (r1 >= 0) {
nums1[w] = nums1[r1--];
} else {
nums1[w] = nums2[r2--];
}
}
}
}

0 comments on commit 0a318a2

Please sign in to comment.