Skip to content

Commit 90d0a21

Browse files
refactor 154
1 parent c0a8003 commit 90d0a21

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

src/main/java/com/fishercoder/solutions/_154.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,51 @@
22

33
/**
44
* 154. Find Minimum in Rotated Sorted Array II
5-
*
6-
* Follow up for "Find Minimum in Rotated Sorted Array":
7-
What if duplicates are allowed?
85
9-
Would this affect the run-time complexity? How and why?
106
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
117
12-
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
8+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
139
1410
Find the minimum element.
1511
1612
The array may contain duplicates.
13+
14+
Example 1:
15+
Input: [1,3,5]
16+
Output: 1
17+
18+
Example 2:
19+
Input: [2,2,2,0,1]
20+
Output: 0
21+
22+
Note:
23+
This is a follow up problem to Find Minimum in Rotated Sorted Array.
24+
Would allow duplicates affect the run-time complexity? How and why?
25+
1726
*/
1827
public class _154 {
28+
public static class Solution1 {
1929
public int findMin(int[] nums) {
20-
int left = 0;
21-
int right = nums.length - 1;
22-
if (nums[left] < nums[right]) {
23-
return nums[left];
24-
}
25-
int min = nums[0];
26-
while (left + 1 < right) {
27-
int mid = left + (right - left) / 2;
28-
min = Math.min(min, nums[mid]);
29-
if (nums[mid] > nums[left]) {
30-
min = Math.min(nums[left], min);
31-
left = mid + 1;
32-
} else if (nums[mid] < nums[left]) {
33-
right = mid - 1;
34-
} else {
35-
left++;
36-
}
30+
int left = 0;
31+
int right = nums.length - 1;
32+
if (nums[left] < nums[right]) {
33+
return nums[left];
34+
}
35+
int min = nums[0];
36+
while (left + 1 < right) {
37+
int mid = left + (right - left) / 2;
38+
min = Math.min(min, nums[mid]);
39+
if (nums[mid] > nums[left]) {
40+
min = Math.min(nums[left], min);
41+
left = mid + 1;
42+
} else if (nums[mid] < nums[left]) {
43+
right = mid - 1;
44+
} else {
45+
left++;
3746
}
38-
min = Math.min(min, Math.min(nums[left], nums[right]));
39-
return min;
47+
}
48+
min = Math.min(min, Math.min(nums[left], nums[right]));
49+
return min;
4050
}
51+
}
4152
}

src/test/java/com/fishercoder/_154Test.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/25/17.
11-
*/
129
public class _154Test {
13-
private static _154 test;
14-
private static int[] nums;
10+
private static _154.Solution1 solution1;
11+
private static int[] nums;
1512

16-
@BeforeClass
17-
public static void setup() {
18-
test = new _154();
19-
}
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _154.Solution1();
16+
}
2017

21-
@Test
22-
public void test1() {
23-
nums = new int[]{1, 1, 1};
24-
assertEquals(1, test.findMin(nums));
25-
}
18+
@Test
19+
public void test1() {
20+
nums = new int[] {1, 1, 1};
21+
assertEquals(1, solution1.findMin(nums));
22+
}
2623
}

0 commit comments

Comments
 (0)