Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Explanation:
- Using a modified binary search to complete this in O(nlogn) time.
- */
- class Solution {
- public int findMin(int[] nums) {
- int l = 0;
- int r = nums.length-1;
- if (nums[0] <= nums[nums.length-1]) return nums[0];
- while (l < r) {
- int m = (l+r)/2;
- if (m != 0 && nums[m] < nums[m-1]) return nums[m];
- if (m != nums.length-1 && nums[m] > nums[m+1]) return nums[m+1];
- // On the left side and need to move right to hit pivot
- if (nums[m] > nums[l]) {
- l += 1;
- } else if (nums[m] < nums[l]) {
- r -= 1;
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement