Skip to content

Commit 85971cb

Browse files
committed
LeetCode 154: Find Minimum in Rotated Sorted Array II
1 parent e855fc1 commit 85971cb

4 files changed

Lines changed: 128 additions & 1 deletion

File tree

src/main/java/org/gd/leetcode/p0153/Solution.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
77
*
88
* @author Horkhover Dmytro
9+
* @see org.gd.leetcode.p0154.Solution
910
* @since 2020-07-26
1011
*/
12+
@SuppressWarnings({"JavadocReference", "DuplicatedCode"})
1113
@LeetCode(
1214
name = "Find Minimum in Rotated Sorted Array",
1315
difficulty = LeetCode.Level.MEDIUM,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.gd.leetcode.p0154;
2+
3+
import org.gd.leetcode.common.LeetCode;
4+
5+
/**
6+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
7+
*
8+
* @author Horkhover Dmytro
9+
* @see org.gd.leetcode.p0153.Solution
10+
* @since 2020-07-26
11+
*/
12+
@SuppressWarnings({"JavadocReference", "DuplicatedCode"})
13+
@LeetCode(
14+
name = "Find Minimum in Rotated Sorted Array II",
15+
difficulty = LeetCode.Level.HARD,
16+
state = LeetCode.State.DONE,
17+
tags = {
18+
LeetCode.Tags.ARRAY,
19+
LeetCode.Tags.BINARY_SEARCH
20+
}
21+
)
22+
class Solution {
23+
24+
private int[] nums;
25+
26+
private static int mid(int left, int right) {
27+
return (left + right + 1) >> 1;
28+
}
29+
30+
private int findMin(int leftIndex, int rightIndex) {
31+
32+
while (true) {
33+
34+
if (leftIndex == rightIndex || nums[leftIndex] < nums[rightIndex])
35+
return nums[leftIndex];
36+
37+
if (rightIndex - leftIndex == 1)
38+
return Math.min(nums[leftIndex], nums[rightIndex]);
39+
40+
final int midIndex = mid(leftIndex, rightIndex);
41+
42+
if (nums[midIndex] == nums[leftIndex]) {
43+
int leftMin = findMin(leftIndex, midIndex);
44+
int rightMin = findMin(midIndex, rightIndex);
45+
return Math.min(leftMin, rightMin);
46+
}
47+
48+
if (nums[midIndex] < nums[leftIndex]) {
49+
rightIndex = midIndex;
50+
} else {
51+
leftIndex = midIndex;
52+
}
53+
}
54+
}
55+
56+
public int findMin(int[] nums) {
57+
if (nums == null) throw new NullPointerException();
58+
if (nums.length == 0) throw new IllegalArgumentException();
59+
60+
switch (nums.length) {
61+
case 1: return nums[0];
62+
case 2: return Math.min(nums[0], nums[1]);
63+
case 3: return Math.min(nums[0], Math.min(nums[2], nums[1]));
64+
case 4: return Math.min(Math.min(nums[0], nums[1]), Math.min(nums[2], nums[3]));
65+
}
66+
67+
this.nums = nums;
68+
69+
return findMin(0, nums.length - 1);
70+
}
71+
}

src/test/java/org/gd/leetcode/p0153/SolutionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.params.provider.MethodSource;
77

88
import java.util.concurrent.TimeUnit;
9+
import java.util.stream.IntStream;
910
import java.util.stream.Stream;
1011

1112
import static org.junit.jupiter.api.Assertions.*;
@@ -16,6 +17,7 @@
1617
* @author Horkhover Dmytro
1718
* @since 2020-07-26
1819
*/
20+
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
1921
@DisplayName("LeetCode #153: Find Minimum in Rotated Sorted Array")
2022
class SolutionTest {
2123

@@ -28,6 +30,9 @@ private static Stream<Arguments> args() {
2830
Arguments.of(new int[]{2, 2, 2, 2, 2, 3, 1, 2, 2}, 1),
2931
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 1, 2, 2}, 1),
3032
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 0, 1, 2, 2}, 0),
33+
Arguments.of(
34+
IntStream.concat(IntStream.rangeClosed(1010, 5_000), IntStream.rangeClosed(14, 1_000)).toArray(),
35+
14),
3136
Arguments.of(new int[]{1}, 1),
3237
Arguments.of(new int[]{1, 0}, 0),
3338
Arguments.of(new int[]{1, 0, -1}, -1),
@@ -39,7 +44,6 @@ private static Stream<Arguments> args() {
3944
@ParameterizedTest
4045
@MethodSource("args")
4146
@DisplayName("FindMin")
42-
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
4347
void test_FindMin(int[] nums, int expected) {
4448
assertEquals(expected, new Solution().findMin(nums));
4549
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.gd.leetcode.p0154;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.stream.IntStream;
10+
import java.util.stream.Stream;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
/**
15+
* Test for {@link Solution}
16+
*
17+
* @author Horkhover Dmytro
18+
* @since 2020-07-26
19+
*/
20+
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
21+
@DisplayName("LeetCode #154: Find Minimum in Rotated Sorted Array II")
22+
class SolutionTest {
23+
24+
private static Stream<Arguments> args() {
25+
return Stream.of(
26+
Arguments.of(new int[]{1, 2, 3, 4, 5}, 1),
27+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6}, 1),
28+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7}, 1),
29+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 1),
30+
Arguments.of(new int[]{2, 2, 2, 2, 2, 3, 1, 2, 2}, 1),
31+
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 1, 2, 2}, 1),
32+
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 0, 1, 2, 2}, 0),
33+
Arguments.of(
34+
IntStream.concat(IntStream.rangeClosed(1010, 5_000), IntStream.rangeClosed(14, 1_000)).toArray(),
35+
14),
36+
Arguments.of(new int[]{1}, 1),
37+
Arguments.of(new int[]{1, 0}, 0),
38+
Arguments.of(new int[]{1, 0, -1}, -1),
39+
Arguments.of(new int[]{3, 4, 5, 1, 2}, 1),
40+
Arguments.of(new int[]{2, 2, 2, 2, 2, 1, 2, 2}, 1),
41+
Arguments.of(new int[]{4, 5, 6, 7, 0, 1, 2}, 0));
42+
}
43+
44+
@ParameterizedTest
45+
@MethodSource("args")
46+
@DisplayName("FindMin")
47+
void test_FindMin(int[] nums, int expected) {
48+
assertEquals(expected, new Solution().findMin(nums));
49+
}
50+
}

0 commit comments

Comments
 (0)