File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ My solutions to LeetCode problems in Kotlin.
1515
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
1616
| [54](https://leetcode.com/problems/spiral-matrix/) | [Spiral Matrix](src/main/kotlin/com/schmoczer/leetcode/_0054/SpiralMatrix.kt) | Medium |
1717
| [73](https://leetcode.com/problems/set-matrix-zeroes/) | [Set Matrix Zeroes](src/main/kotlin/com/schmoczer/leetcode/_0073/SetMatrixZeroes.kt) | Medium |
18+
| [88](https://leetcode.com/problems/merge-sorted-array/) | [Merge Sorted Array](src/main/kotlin/com/schmoczer/leetcode/_0088/MergeSortedArray.kt) | Easy |
1819
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
1920
| [141](https://leetcode.com/problems/linked-list-cycle/) | [Linked List Cycle](src/main/kotlin/com/schmoczer/leetcode/_0141/LinkedListCycle.kt) | Easy |
2021
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.schmoczer.leetcode._0088
2+
3+
class MergeSortedArray {
4+
// Runtime 0ms Beats 100.00%
5+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int) {
6+
val copied = IntArray(m)
7+
System.arraycopy(nums1, 0, copied, 0, m)
8+
var i = 0
9+
var j = 0
10+
while (i < m || j < n) {
11+
if (i == m) {
12+
nums1[i + j] = nums2[j]
13+
j++
14+
continue
15+
}
16+
if (j == n) {
17+
nums1[i + j] = copied[i]
18+
i++
19+
continue
20+
}
21+
22+
val number1 = copied[i]
23+
val number2 = nums2[j]
24+
if (number1 < number2) {
25+
nums1[i + j] = number1
26+
i++
27+
} else {
28+
nums1[i + j] = number2
29+
j++
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Merge Sorted Array
2+
3+
You are given two integer arrays `nums1` and `nums2`, sorted in non-decreasing order, and two integers `m` and `n`,
4+
representing the number of elements in `nums1` and `nums2` respectively.
5+
6+
Merge `nums1` and `nums2` into a single array sorted in non-decreasing order.
7+
8+
The final sorted array should not be returned by the function, but instead be stored inside the array `nums1`. To
9+
accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be
10+
merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
11+
12+
Example 1:
13+
14+
> Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
15+
>
16+
> Output: [1,2,2,3,5,6]
17+
18+
Example 2:
19+
20+
> Input: nums1 = [1], m = 1, nums2 = [], n = 0
21+
>
22+
> Output: [1]
23+
24+
Example 3:
25+
26+
> Input: nums1 = [0], m = 0, nums2 = [1], n = 1
27+
>
28+
> Output: [1]
29+
30+
Constraints:
31+
32+
- `nums1.length == m + n`
33+
- `nums2.length == n`
34+
- `0 <= m, n <= 200`
35+
- `1 <= m + n <= 200`
36+
- `-10^9 <= nums1[i], nums2[j] <= 10^9`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.schmoczer.leetcode._0088
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import kotlin.test.assertContentEquals
8+
9+
class MergeSortedArrayTest {
10+
private companion object {
11+
@JvmStatic
12+
fun sortedArrays() = listOf(
13+
Arguments.of(intArrayOf(1, 2, 3, 0, 0, 0), 3, intArrayOf(2, 5, 6), 3, intArrayOf(1, 2, 2, 3, 5, 6)),
14+
Arguments.of(intArrayOf(1), 1, intArrayOf(), 0, intArrayOf(1)),
15+
Arguments.of(intArrayOf(0), 0, intArrayOf(1), 1, intArrayOf(1)),
16+
)
17+
}
18+
19+
private lateinit var sut: MergeSortedArray
20+
21+
@BeforeEach
22+
fun setUp() {
23+
sut = MergeSortedArray()
24+
}
25+
26+
@ParameterizedTest(name = "{0} and {2} should be merged into {4}")
27+
@MethodSource("sortedArrays")
28+
fun `two integer arrays should be merged into the first one and be sorted`(
29+
nums1: IntArray,
30+
m: Int,
31+
nums2: IntArray,
32+
n: Int,
33+
expected: IntArray,
34+
) {
35+
sut.merge(nums1, m, nums2, n)
36+
37+
assertContentEquals(expected, nums1)
38+
}
39+
}

0 commit comments

Comments
 (0)