Find Minimum in rotated Sorted Array leetcode java

Source: Internet
Author: User


Topic:

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.

Assume no duplicate exists in the array.

Problem Solving Ideas:

First, suppose that an array of sorted without rotated is [all-in-one], assuming that we rotate the array through a pivot, then the result may be: [2,3,1], [3,1,2], can be found: Num[low] always greater than (or equal to) num[ High]. Because you were previously an array of sorted, you looked for a pivot in a sorted array to rotate, so for example, the value behind the pivot is greater than the value before the pivot. So based on this discovery, and the dichotomy method to find out. We can solve the problem according to the following judgment. Num[mid] There are two possibilities, if NUM[MID] > Num[high], proving Num[mid] in the interval after rotated, this interval we have just known is greater than the value before pivot, so the minimum value is within the low=mid+1 that interval. Another possibility, Num[mid] <= Num[high], then we can just see that this possibility illustrates Mid~high and is ordered, so the minimum value is within the High=mid range (mid may be the minimum). The minimum value can be found based on this judgment.

The code is as follows:

1 PublicintFindmin (int[] num) {
2intLow = 0, high = num.length-1;
3 while(Low < High && Num[low] >= Num[high]) {
4intMid = (low + high)/2;
5if(Num[mid] > Num[high])
6Low = mid + 1;
7Else
8High = mid;
9}
TenreturnNum[low];
One}

Find Minimum in rotated Sorted Array leetcode java

Related Article

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.