From 57cc0e60890a726f059b164722954ea73df2cd56 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sat, 13 Sep 2025 11:01:40 +0800 Subject: [PATCH] Add solution for the "Merge Sorted Array" problem with unit tests. --- src/MergeSortedArray88.java | 22 ----- .../com/leetcode/easy/MergeSortedArray88.java | 45 +++++++++++ .../leetcode/easy/MergeSortedArray88Test.java | 80 +++++++++++++++++++ 3 files changed, 125 insertions(+), 22 deletions(-) delete mode 100644 src/MergeSortedArray88.java create mode 100644 src/main/java/com/leetcode/easy/MergeSortedArray88.java create mode 100644 src/test/java/com/leetcode/easy/MergeSortedArray88Test.java diff --git a/src/MergeSortedArray88.java b/src/MergeSortedArray88.java deleted file mode 100644 index c480e2d..0000000 --- a/src/MergeSortedArray88.java +++ /dev/null @@ -1,22 +0,0 @@ -public class MergeSortedArray88 { - public void merge(int[] nums1, int m, int[] nums2, int n) { - int last = m + n - 1; - m--; - n--; - while (m >= 0 && n >= 0) { - if (nums1[m] > nums2[n]) { - nums1[last] = nums1[m]; - m--; - } else { - nums1[last] = nums2[n]; - n--; - } - last--; - } - while (n >= 0) { - nums1[last] = nums2[n]; - n--; - last--; - } - } -} diff --git a/src/main/java/com/leetcode/easy/MergeSortedArray88.java b/src/main/java/com/leetcode/easy/MergeSortedArray88.java new file mode 100644 index 0000000..41557ce --- /dev/null +++ b/src/main/java/com/leetcode/easy/MergeSortedArray88.java @@ -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--; + } + } +} diff --git a/src/test/java/com/leetcode/easy/MergeSortedArray88Test.java b/src/test/java/com/leetcode/easy/MergeSortedArray88Test.java new file mode 100644 index 0000000..8478971 --- /dev/null +++ b/src/test/java/com/leetcode/easy/MergeSortedArray88Test.java @@ -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); + } +}