Skip to content

Commit 8e97919

Browse files
[LEET-153] add 153
1 parent d5218be commit 8e97919

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReadNCharactersGivenRead4.java)| O(n)|O(1) | Easy|
235235
|156|[Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeUpsideDown.java)| O(n)|O(h) | Medium| Tree, Recursion
236236
|155|[Min Stack](https://leetcode.com/problems/min-stack/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinStack.java)| O(1)|O(n) | Easy| Stack
237+
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindMinimuminRotatedSortedArray.java)| O(logn)|O(1) | Medium| Array, Binary Search
237238
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MaximumProductSubarray.java)| O(n)|O(1) | Medium| Array
238239
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseWordsinaString.java)| O(n)|O(n) | Medium|
239240
|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EvaluateReversePolishNotation.java)| O(?)|O(?) | Medium
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5+
6+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
7+
8+
Find the minimum element.
9+
10+
You may assume no duplicate exists in the array.
11+
*/
12+
public class FindMinimuminRotatedSortedArray {
13+
14+
public int findMin(int[] nums) {
15+
int left = 0;
16+
int right = nums.length-1;
17+
if (nums[left] < nums[right]) return nums[left];
18+
int min = nums[0];
19+
while (left + 1 < right) {
20+
int mid = left + (right-left)/2;
21+
min = Math.min(min, nums[mid]);
22+
if (nums[mid] > nums[left]) {
23+
min = Math.min(nums[left], min);
24+
left = mid+1;
25+
}
26+
else if (nums[mid] < nums[left]) {
27+
right = mid-1;
28+
}
29+
}
30+
min = Math.min(min, Math.min(nums[left], nums[right]));
31+
return min;
32+
}
33+
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.FindMinimuminRotatedSortedArray;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 1/10/17.
12+
*/
13+
public class FindMinimuminRotatedSortedArrayTest {
14+
private static FindMinimuminRotatedSortedArray test;
15+
private static int expected;
16+
private static int actual;
17+
private static int[] nums;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new FindMinimuminRotatedSortedArray();
22+
}
23+
24+
@Before
25+
public void setupForEachTest(){}
26+
27+
@Test
28+
public void test1(){
29+
30+
nums = new int[]{4, 5, 6, 7, 0, 1, 2};
31+
expected = 0;
32+
actual = test.findMin(nums);
33+
assertEquals(expected, actual);
34+
35+
}
36+
37+
@Test
38+
public void test2(){
39+
nums = new int[]{1};
40+
expected = 1;
41+
actual = test.findMin(nums);
42+
assertEquals(expected, actual);
43+
}
44+
45+
@Test
46+
public void test3(){
47+
nums = new int[]{2, 1};
48+
expected = 1;
49+
actual = test.findMin(nums);
50+
assertEquals(expected, actual);
51+
}
52+
53+
@Test
54+
public void test4(){
55+
nums = new int[]{2,3,4,5,1};
56+
expected = 1;
57+
actual = test.findMin(nums);
58+
assertEquals(expected, actual);
59+
}
60+
}

0 commit comments

Comments
 (0)