forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumInRotatedSortedArray.java
More file actions
31 lines (27 loc) · 947 Bytes
/
MinimumInRotatedSortedArray.java
File metadata and controls
31 lines (27 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Time Complexity : O(log n).
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No. The core part of finding that minimum lies in the unsorted area of the area helped in
// effectively solving the problem along with couple of straight forward cases.
// Your code here along with comments explaining your approach
class Solution {
public int findMin(int[] nums) {
int low = 0;
int high = nums.length-1;
while(low <= high){
int mid = low + (high-low)/2;
if(nums[low] <= nums[high]){
return nums[low];
}
if(mid > 0 && nums[mid-1] > nums[mid]){
return nums[mid];
}
if(nums[mid] >= nums[low]){
low = mid + 1;
}else{
high = mid - 1;
}
}
return Integer.MAX_VALUE;
}
}