Skip to content

Commit c9a9e65

Browse files
author
yennj12
committed
update 88 java
1 parent 34cc116 commit c9a9e65

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

data/progress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- 5 / day -> 15 days to finish
77
- 2 pointers
88
- again: 680
9-
- similar:
9+
- similar: 2516
1010
- todo:
1111
- add neetcode algo linkedin post
1212
- re-do all previous `again` problems, similar problems, make notes

leetcode_java/src/main/java/LeetCodeJava/Sort/MergeSortedArray.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,87 @@
11
package LeetCodeJava.Sort;
22

3-
// https://leetcode.com/problems/merge-sorted-array/
4-
53
import java.util.Arrays;
64

5+
// https://leetcode.com/problems/merge-sorted-array/
6+
/**
7+
* 88. Merge Sorted Array
8+
* Solved
9+
* Easy
10+
* Topics
11+
* Companies
12+
* Hint
13+
* You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
14+
*
15+
* Merge nums1 and nums2 into a single array sorted in non-decreasing order.
16+
*
17+
* The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
24+
* Output: [1,2,2,3,5,6]
25+
* Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
26+
* The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
27+
* Example 2:
28+
*
29+
* Input: nums1 = [1], m = 1, nums2 = [], n = 0
30+
* Output: [1]
31+
* Explanation: The arrays we are merging are [1] and [].
32+
* The result of the merge is [1].
33+
* Example 3:
34+
*
35+
* Input: nums1 = [0], m = 0, nums2 = [1], n = 1
36+
* Output: [1]
37+
* Explanation: The arrays we are merging are [] and [1].
38+
* The result of the merge is [1].
39+
* Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
40+
*
41+
*
42+
* Constraints:
43+
*
44+
* nums1.length == m + n
45+
* nums2.length == n
46+
* 0 <= m, n <= 200
47+
* 1 <= m + n <= 200
48+
* -109 <= nums1[i], nums2[j] <= 109
49+
*
50+
*
51+
* Follow up: Can you come up with an algorithm that runs in O(m + n) time?
52+
*
53+
*
54+
*/
755
public class MergeSortedArray {
856

957
// V0
58+
// IDEA: ARRAY OP + SORT
59+
public void merge(int[] nums1, int m, int[] nums2, int n) {
60+
/**
61+
* NOTE !!! we loop from 0 to `n`
62+
*/
63+
for (int i = 0; i < n; i++) {
64+
/**
65+
* NOTE !!! we assign nums2[i] val to `i+m` idx at nums1 (nums1[i + m] )
66+
*/
67+
nums1[i + m] = nums2[i];
68+
}
69+
// NOTE: we sort merged array
70+
Arrays.sort(nums1);
71+
}
1072

1173
// V1
1274
// IDEA : MERGE AND SORT
1375
// https://leetcode.com/problems/merge-sorted-array/editorial/
76+
/**
77+
* NOTE !!!
78+
*
79+
* two integers m and n,
80+
* representing the number of elements in nums1 and nums2 respectively.
81+
*
82+
* -> m : nums1 element cnt
83+
* -> n : nums2 element cnt
84+
*/
1485
public void merge_2(int[] nums1, int m, int[] nums2, int n) {
1586
for (int i = 0; i < n; i++) {
1687
nums1[i + m] = nums2[i];

leetcode_java/src/main/java/dev/workspace8.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9471,4 +9471,30 @@ public String mergeAlternately(String word1, String word2) {
94719471
}
94729472

94739473

9474+
// LC 88
9475+
// 10.38 - 10.48 am
9476+
/**
9477+
* Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
9478+
* -> Output: [1,2,2,3,5,6]
9479+
*
9480+
*
9481+
* n1 = [1,2,2,5,3,0]
9482+
* 1 2 3 4
9483+
*
9484+
* n2 = [2.5.6]
9485+
* 1 2 3
9486+
*
9487+
*
9488+
* [1,
9489+
*
9490+
*
9491+
*
9492+
*
9493+
*
9494+
*
9495+
*/
9496+
public void merge(int[] nums1, int m, int[] nums2, int n) {
9497+
9498+
}
9499+
94749500
}

0 commit comments

Comments
 (0)