Skip to content

Commit e855fc1

Browse files
LeetCode 153: Find Minimum in Rotated Sorted Array
1 parent 6522538 commit e855fc1

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.gd.leetcode.p0153;
2+
3+
import org.gd.leetcode.common.LeetCode;
4+
5+
/**
6+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
7+
*
8+
* @author Horkhover Dmytro
9+
* @since 2020-07-26
10+
*/
11+
@LeetCode(
12+
name = "Find Minimum in Rotated Sorted Array",
13+
difficulty = LeetCode.Level.MEDIUM,
14+
state = LeetCode.State.DONE,
15+
tags = {
16+
LeetCode.Tags.ARRAY,
17+
LeetCode.Tags.BINARY_SEARCH
18+
}
19+
)
20+
class Solution {
21+
22+
private int[] nums;
23+
24+
private static int mid(int left, int right) {
25+
return (left + right + 1) >> 1;
26+
}
27+
28+
private int findMin(int leftIndex, int rightIndex) {
29+
30+
while (true) {
31+
32+
if (leftIndex == rightIndex || nums[leftIndex] < nums[rightIndex])
33+
return nums[leftIndex];
34+
35+
if (rightIndex - leftIndex == 1)
36+
return Math.min(nums[leftIndex], nums[rightIndex]);
37+
38+
final int midIndex = mid(leftIndex, rightIndex);
39+
40+
if (nums[midIndex] == nums[leftIndex]) {
41+
int leftMin = findMin(leftIndex, midIndex);
42+
int rightMin = findMin(midIndex, rightIndex);
43+
return Math.min(leftMin, rightMin);
44+
}
45+
46+
if (nums[midIndex] < nums[leftIndex]) {
47+
rightIndex = midIndex;
48+
} else {
49+
leftIndex = midIndex;
50+
}
51+
}
52+
}
53+
54+
public int findMin(int[] nums) {
55+
if (nums == null) throw new NullPointerException();
56+
if (nums.length == 0) throw new IllegalArgumentException();
57+
58+
switch (nums.length) {
59+
case 1: return nums[0];
60+
case 2: return Math.min(nums[0], nums[1]);
61+
case 3: return Math.min(nums[0], Math.min(nums[2], nums[1]));
62+
case 4: return Math.min(Math.min(nums[0], nums[1]), Math.min(nums[2], nums[3]));
63+
}
64+
65+
this.nums = nums;
66+
67+
return findMin(0, nums.length - 1);
68+
}
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.gd.leetcode.p0153;
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.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
/**
14+
* Test for {@link Solution}
15+
*
16+
* @author Horkhover Dmytro
17+
* @since 2020-07-26
18+
*/
19+
@DisplayName("LeetCode #153: Find Minimum in Rotated Sorted Array")
20+
class SolutionTest {
21+
22+
private static Stream<Arguments> args() {
23+
return Stream.of(
24+
Arguments.of(new int[]{1, 2, 3, 4, 5}, 1),
25+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6}, 1),
26+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7}, 1),
27+
Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 1),
28+
Arguments.of(new int[]{2, 2, 2, 2, 2, 3, 1, 2, 2}, 1),
29+
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 1, 2, 2}, 1),
30+
Arguments.of(new int[]{2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 0, 1, 2, 2}, 0),
31+
Arguments.of(new int[]{1}, 1),
32+
Arguments.of(new int[]{1, 0}, 0),
33+
Arguments.of(new int[]{1, 0, -1}, -1),
34+
Arguments.of(new int[]{3, 4, 5, 1, 2}, 1),
35+
Arguments.of(new int[]{2, 2, 2, 2, 2, 1, 2, 2}, 1),
36+
Arguments.of(new int[]{4, 5, 6, 7, 0, 1, 2}, 0));
37+
}
38+
39+
@ParameterizedTest
40+
@MethodSource("args")
41+
@DisplayName("FindMin")
42+
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
43+
void test_FindMin(int[] nums, int expected) {
44+
assertEquals(expected, new Solution().findMin(nums));
45+
}
46+
}

0 commit comments

Comments
 (0)