Leetcode OJ 154. Find Minimum in rotated Sorted Array II

Source: Internet
Author: User

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.

"Problem Analysis"

The array elements in this topic may be duplicated compared to the find Minimum in rotated Sorted array topic.

Ideas

In the previous topic, we used a binary lookup to find the breakpoint in the array, thus determining the position of the minimum value. In the absence of duplicate elements, you can compare the size of the median and boundary values to determine the position of the median (that is, in the first or second half of the array), thus determining the position of the minimum and intermediate values. However, if there are duplicate elements that will cause us to be unable to determine the position of the median and minimum values, for example: [10,1,10,10] and [10,10,1,10], the median value of the two arrays is 10, but we cannot tell if the minimum is on the left or the right of the middle value, Because both of these situations are likely to occur. But we can be sure that there must be a total of 10 on the left or right side of the middle value, and the minimum value is in another paragraph. 1. Recursion if this is the case, we are looking at the left and right sides of the median, and the benefit of recursion is that even if two arrays are to be searched, the overall time complexity is good. 2. Non-recursive, delete boundary value in the case where the median and boundary values are equal, the search in the array after the deletion of the boundary value does not affect our search results. "Java code" non-recursive
1  Public classSolution {2      Public intFindmin (int[] nums) {3         intStart=0,mid=0,end=nums.length-1;4          while(start<end) {5             if(Nums[start] < nums[end])returnNums[start];6             7mid=start+ (End-start)/2;8             if(Nums[mid]>nums[end]) start=mid+1;9             Else if(Nums[mid]==nums[end]) end--;Ten             ElseEnd=mid; One         } A         returnNums[start]; -     } -}
"Java Code" recursion
1  Public classSolution {2      Public intFindmin (int[] nums) {3         intLen =nums.length;4         if(len = = 1)returnNums[0];5 6         returnFind (nums, 0, len-1);7     }8     9      Public intFindintNums[],intLeftintRight ) {Ten          while(Left <Right ) { One             if(Nums[left] < nums[right])returnNums[left]; A             if(left = = right-1)returnmath.min (Nums[left], nums[right]); -              -             intMid = left + (right-left)/2; the             if(Nums[left] = = Nums[mid] && Nums[mid] = =Nums[right]) -                 returnMath.min (Find (Nums, left, mid-1), find (Nums, mid+1, right)); -                  -             if(Nums[left] <= nums[mid]) left = mid + 1; +             Elseright =mid; -         } +         returnNums[left]; A     } at}

Leetcode OJ 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.