Skip to content

Commit 315365d

Browse files
committed
Find Minimum In Rotated Sorted Array
1 parent 78bec4d commit 315365d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(log n)
2+
// Using binary search, it takes `log n` time complexity, n indicates the length of the given array nums
3+
// SC: O(1)
4+
// constant space occupation
5+
class Solution {
6+
public int findMin(int[] nums) {
7+
8+
int start = 0;
9+
int end = nums.length - 1;
10+
int min = Integer.MAX_VALUE;
11+
12+
while (start <= end) {
13+
int mid = start + (end - start) / 2;
14+
15+
if (nums[start] <= nums[mid]) {
16+
min = Math.min(min, nums[start]);
17+
start = mid + 1;
18+
} else if (nums[mid] <= nums[end]) {
19+
min = Math.min(min, nums[mid]);
20+
end = mid - 1;
21+
}
22+
}
23+
return min;
24+
}
25+
}

0 commit comments

Comments
 (0)