Find Minimum in Rotated Sorted Array | LeetCode 153 | C++, Java, Python3

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

Facebook Coding Interview question,
google coding interview question,
leetcode,
Find Minimum in Rotated Sorted Array,
Find Minimum in Rotated Sorted Array c++,
Find Minimum in Rotated Sorted Array Java,
Find Minimum in Rotated Sorted Array python,
Find Minimum in Rotated Sorted Array solution,
153. Find Minimum in Rotated Sorted Array

#CodingInterview #LeetCode #Google #Amazon #RotatedSortedArray
Рекомендации по теме
Комментарии
Автор

As you are using,
while (l<r) {
}
But the last case when nums[mid] == nums [r] will never occur bcoz l==r won't exist in loop.

vaibhavmalik
Автор

Nice and logical and simple as always, best channel for leetcode solutions. pen down

guptashashwat
Автор

Why do we have to compare to array[right] but not array[left]?

yulsic
Автор

How to come up with such solutions taking into consideration the different scenarios??

medhak
Автор

How can we do all this by comparing mid by left, instead of comparing it with the right. As this is not obvious to compare it with right alone, please suggest how to do it by comparing mid with left.
I gave a try, but it fails in some cases:

public int findMin(int[] nums, int left, int right) {
if(left == right) return nums[right];
int mid = left + (right - left) / 2;

if(nums[mid] > nums[left]){
if(nums[mid] < nums[right])
return findMin(nums, left, mid-1);
if(nums[mid] > nums[right])
return findMin(nums, mid+1, right);
}

if(nums[mid] < nums[left]) return findMin(nums, left, mid);
return findMin(nums, left+1, right);
}

Please help me do it using left, as I started off with left, and this can happen at the time of the interview. Thanks a lot for your videos.

chandreshsikarwar
Автор

easy solution..
class Solution {
public:
int findMin(vector<int>& nums ) {

int l=0;
int h=nums.size() - 1;
while(l<h){
int mid= l + (h-l)/2;
if (nums[mid]<nums[h]){
h=mid;
}
else{
l=mid+1;
}
}
return nums[l];
}
};

kanikagupta