Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/MergeSortedArray88.java

This file was deleted.

45 changes: 45 additions & 0 deletions src/main/java/com/leetcode/easy/MergeSortedArray88.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Tags: Array, Two Pointers
package com.leetcode.easy;

public class MergeSortedArray88 {
/*
Time complexity: O(m + n)
Breakdown:
- First while loop: Compares elements from both arrays and places them in the correct position
- In each iteration, either p1 or p2 decrements by 1
- The loop continues until one of the pointers reaches -1
- Maximum iterations: min(m, n)
- Second while loop: Copies remaining elements from nums2 (if any)
- Runs at most n iterations (when all elements from nums2 need to be copied)
- Total operations: Each element from both arrays is processed exactly once
- Overall: O(m + n)
Where m is the number of valid elements in nums1 and n is the number of elements in nums2.

Space complexity: O(1)
Breakdown:
- In-place modification: The algorithm modifies nums1 directly without using additional arrays
- Pointer variables: p1, p2, and index use constant space O(1)
- No extra data structures: No additional arrays, lists, or collections are created
- Overall: O(1) - constant space complexity
*/
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1;
int p2 = n - 1;
int index = m + n - 1;
while (p1 >= 0 && p2 >= 0) {
if (nums1[p1] >= nums2[p2]) {
nums1[index] = nums1[p1];
p1--;
} else {
nums1[index] = nums2[p2];
p2--;
}
index--;
}
while (p2 >= 0) {
nums1[index] = nums2[p2];
p2--;
index--;
}
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/leetcode/easy/MergeSortedArray88Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.leetcode.easy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class MergeSortedArray88Test {
private MergeSortedArray88 solution;

@BeforeEach
void setUp() {
solution = new MergeSortedArray88();
}

@Test
void testMerge_Example1() {
int[] nums1 = {1, 2, 3, 0, 0, 0};
int m = 3;
int[] nums2 = {2, 5, 6};
int n = 3;

solution.merge(nums1, m, nums2, n);

int[] expected = {1, 2, 2, 3, 5, 6};
assertArrayEquals(expected, nums1);
}

@Test
void testMerge_Example2() {
int[] nums1 = {1};
int m = 1;
int[] nums2 = {};
int n = 0;

solution.merge(nums1, m, nums2, n);

int[] expected = {1};
assertArrayEquals(expected, nums1);
}

@Test
void testMerge_Example3() {
int[] nums1 = {0};
int m = 0;
int[] nums2 = {1};
int n = 1;

solution.merge(nums1, m, nums2, n);

int[] expected = {1};
assertArrayEquals(expected, nums1);
}

@Test
void testMerge_Example4() {
int[] nums1 = {4, 5, 6, 0, 0, 0};
int m = 3;
int[] nums2 = {1, 2, 3};
int n = 3;

solution.merge(nums1, m, nums2, n);

int[] expected = {1, 2, 3, 4, 5, 6};
assertArrayEquals(expected, nums1);
}

@Test
void testMerge_Example5() {
int[] nums1 = {1, 2, 3, 0, 0, 0};
int m = 3;
int[] nums2 = {4, 5, 6};
int n = 3;

solution.merge(nums1, m, nums2, n);

int[] expected = {1, 2, 3, 4, 5, 6};
assertArrayEquals(expected, nums1);
}
}