Skip to content

Instantly share code, notes, and snippets.

@oyekanmiayo
Last active August 9, 2020 06:26
Show Gist options
  • Save oyekanmiayo/99034999aca067c156efe419bb54c1ac to your computer and use it in GitHub Desktop.
Save oyekanmiayo/99034999aca067c156efe419bb54c1ac to your computer and use it in GitHub Desktop.
Solution to " Find Minimum in Rotated Sorted Array II" on Leetcode
class Solution {
// Final solution
public int findMin(int[] nums) {
int start = 0;
int end = nums.length - 1;
while(start < end){
int mid = start + (end - start) / 2;
if(nums[mid] < nums[end]){
end = mid;
} else if (nums[mid] > nums[end]){
start = mid + 1;
} else {
end--;
}
}
return nums[end];
}
}
@oyekanmiayo
Copy link
Author

The worst case is O(N), where all the elements are duplicates. Amortized is O(logn).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment