[leetcode#154] Find Minimum in rotated Sorted Array II

Source: Internet
Author: User

The question:

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.

My First Ugly solution:

 Public classSolution { Public intFindmin (int[] num) {        if(num = =NULL|| Num.length = = 0)            return-1; intLow = 0; intHigh = Num.length-1; intMID =-1;  while(Low <High ) {Mid= (low + high)/2; if(Num[low] = = Num[mid] && Low! =mid) { Low++; Continue; }            if(Num[low] <= Num[mid] && Num[low] >=Num[high]) Low= Mid + 1; Else High=mid; }        returnNum[low]; }}

An improved analysis and solution.

This problem involves a very imporant skills in binary searching. It ' s very very tricky!!! The idea behind Thisproblem is not complex. It could is classified into following three cases:1.a[mid] > A[low], we could discard ThisPart, and search in the range[mid+1, High]2.a[mid] <A[low], the mininum must is included in the first part Range[low, mid]. Note the mid might is the answer, thus we could not discard it.3.a[mid] = = A[low]. Inch This  Casewe could not distinguish which part could hold the anwser and we need to proceed on. One solution is to move low pointer one step forward. We could discard A[low]. The resulting answer is: Public intFindmin (int[] num) {    if(num = =NULL|| Num.length = = 0)        return-1; intLow = 0; intHigh = Num.length-1; intMID =-1;  while(Low <High ) {Mid= (low + high)/2; if(Num[low] >Num[mid]) high=mid; Else if(Num[low] <Num[mid]) Low= Mid + 1; Else Low++; }    returnNum[low];} However ThisSolution has a very big pitfall!!!Because the"Divide operation" onlyreturnThe Interger part "MID = (low + high)/2;", the before exiting from the loop, the low pointer could point to the same element with mid pointer. Consider following, cases:1.3 1low:num[0] = 3, mid:num[0] = 3. Result in low++;returnAnswer num[1] = 1. (the right answer)2.1 3low:num[0] = 3, mid:num[0] = 3. Result in low++;returnAnswer num[1] = 3. (The wrong answer) What a big problem!!!An very elegant-solve Thisproblem. Using the same idea, we compare num[mid] and Num[high], then update the solution into: while(Low <High ) {Mid= (low + high)/2; if(Num[mid] >Num[high]) Low= Mid + 1; Else if(Num[low] <Num[mid]) high=mid; Else High--;}return[Low];//Note we reuturn[low] at here!!!If ThisFix works?1. 3 1high:num[0] = 1, mid:num[0] = 3. Result in high--;returnAnswer num[0] = 1. (the right answer)2.1 3high:num[1] = 3, mid:num[0] = 1. Result in high= Mid = 0;returnAnswer num[0] = 1. (The right answer) The reason is that:the-pointer would not-point-to-the-same element with mid. The invariant holds to the end!!!iff High= = Mid, high must equal to low, since while(Low < High), This  Caseis impossible. Note:the idea behind ThisMethod is, ThisMethod could properly tackle the CaseWhen there is only a elements left. When only a elements left, the low pointer and mid pointer point to the same element. By comparing and the mid pointer, we could guarantee to reach the right answer by Returing Num[low].if(Num[mid] >Num[high]) Low= Mid + 1;Else if(Num[low] <Num[mid]) high=mid;Else High--; When Num[mid] (low)> Num[high], we move the low pointer to high. (low++), A[low] is the smaller. When Num[mid] (low)< Num[high], we keep low on the same position, A[low] is the smaller.

The improved Solution:

 Public classSolution { Public intFindmin (int[] num) {        if(num = =NULL|| Num.length = = 0)            return-1; intLow = 0; intHigh = Num.length-1; intMID =-1;  while(Low <High ) {Mid= (low + high)/2; if(Num[mid] >Num[high]) Low= Mid + 1; Else if(Num[low] <Num[mid]) high=mid; Else High--; }        returnNum[low]; }}

[leetcode#154] Find Minimum in rotated Sorted Array II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.