Skip to content

Commit 62c5425

Browse files
committed
add: find minimum in rotated sorted array solution
1 parent 2c75463 commit 62c5425

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
/*
3+
time complexity: O(log n)
4+
space complexity: O(h)
5+
*/
6+
public int findMin(int[] nums) {
7+
int start = 0, end = nums.length - 1;
8+
9+
if (nums[start] < nums[end]) return nums[start];
10+
11+
while (start < end) {
12+
int mid = start + (end - start) / 2;
13+
14+
if (nums[mid] < nums[end]) {
15+
end = mid;
16+
} else {
17+
start = mid + 1;
18+
}
19+
}
20+
return nums[start];
21+
}
22+
}

0 commit comments

Comments
 (0)