follow to "Find Minimum in rotated Sorted Array":
What if duplicates is allowed?
Would this affect the run-time complexity? How and why?
Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Find the minimum element.
The array may contain duplicates.
153. Find Minimum in rotated Sorted Array, this topic allows for duplicate elements. The original is to rely on the middle and edge elements of the size of the relationship, to determine which half is orderly. Now, because of the repetition of the elements, if you encounter the middle and edge of the same situation, it is impossible to determine which side is ordered, because which can be ordered. Assuming that the original array is {1,2,3,3,3,3,3}, then it may be {3,3,3,3,3,1,2} after rotation, or {3,1,2,3,3,3,3}, when judging the left edge and center is 3, you do not know which half should be cut off. The solution is to move one step towards the edge until the edges and the middle are not equal or meet, which leads to the possibility of not being able to cut in half. So the worst case scenario is one step at a time, moving N in total, the time complexity of the algorithm j:o (LOGN) ~ O (n).
Java:
public int findmin (int[] num) { if (num = = NULL | | num.length==0) return 0; int l = 0; int r = num.length-1; int min = num[0]; while (l<r-1) { int m = (l+r)/2; if (Num[l]<num[m]) { min = math.min (num[l],min); L = m+1; } else if (Num[l]>num[m]) { min = math.min (num[m],min); R = m-1; } else { l++; } } min = Math.min (num[r],min); min = Math.min (num[l],min); return min; }
Python:
Class solution (Object): def findmin (self, Nums): "" " : Type Nums:list[int] : Rtype:int " "" Left, right = 0, Len (nums)-1 while left < right: mid = left + (right-left)/2 if nums[mid] = = Nums[rig HT]: Right-= 1 elif Nums[mid] < Nums[right]: Right = mid- else: Left = mid + 1 return nums [Left]
Python:
Class Solution2 (object): def findmin (self, Nums): "" " : Type Nums:list[int] : Rtype:int " "" Left, right = 0, Len (nums)-1 while left < right and Nums[left] >= Nums[right]: mid = left + (right-left) /2 if nums[mid] = = Nums[left]: Left + + 1 elif Nums[mid] < Nums[left]: Right = Mid else: l EFT = mid + 1 return Nums[left]
C++:
Class Solution {public: int findmin (vector<int> &nums) { if (Nums.empty ()) return 0; int left = 0, right = nums.size ()-1, res = nums[0]; while (left < right-1) { int mid = left + (right-left)/2; if (Nums[left] < Nums[mid]) { res = min (res, nums[left]); left = mid + 1; } else if (Nums[left] > Nums[mid]) { res = min (res, nums[right]); right = Mid; } else ++left; } res = min (res, nums[left]); res = min (res, nums[right]); return res; }};
Similar topics:
[Leetcode] 33. Search in rotated Sorted array searches in an ordered rotation
[Leetcode] 81. Search in rotated Sorted Array II searching in rotated ordered Arrays II
[Leetcode] 153. Find Minimum in rotated Sorted array looking for the minimum value of the rotated ordered arrays
[Leetcode] 154. Find Minimum in rotated Sorted Array II find the minimum value of a rotated ordered array II