Leetcode:find Peak Element

Source: Internet
Author: User

A Peak element is an element, which is greater than its neighbors. Given an input array where num[i]≠num[ireturncasereturn the index to any on E of the peaks is fine. Imagine that num[-1] = num[n] =-∞.for example, in array [return the index number 2. CLI CK to show spoilers. Note:your solution should is in logarithmic complexity.

This problem is still a bit troublesome, ask O (Logn) time complexity, think of using binary search. If the intermediate element is greater than its adjacent successive elements, the left side of the intermediate element (containing the intermediate element) must contain a local maximum value. If the intermediate element is smaller than its adjacent successive elements, the right side of the intermediate element must contain a local maximum value. Until the last left edge meets the right edge

Here are a few things to note:

1. Here the choice of only intermediate element mid with its adjacent subsequent elements mid+1 the size of the discussion, without involving the previous element mid-1, mainly convenient. Mid= (Left+right)/2, this division has a floor effect inside, i.e. ⌊ (left+right)/2⌋. When left and right are not equal, index is both mid and mid+1, but the mid-1 may be less than 0. So every time when mid==0, mid-1 elements have to be discussed in particular, trouble. For example, such as Left==0, Right==1, mid==0, Mid-1==-1, mid+1==1, do not want to discuss mid-1 this situation

2. If the intermediate element is greater than its adjacent successive elements, the left side of the intermediate element (including the intermediate element) must contain a local maximum value, when the middle element is probably the local maximum point, so move R = Mid instead of R = mid-1, and if the intermediate element is smaller than its adjacent subsequent elements, The right side of the middle element must contain a local maximum value. Then the middle element is definitely not the local maximum point, so move L = mid + 1

3. The reason to use the left and right edge encounter as a condition to find, mainly do not want to involve mid-1 problems. Otherwise the condition is num[mid]>num[mid+1] && num[mid]>num[mid-1] and the situation mid==0

4. If mid is a valley, such as [1, 2, 1, 6, 7], then where do I jump? This is actually the right jump to the left. You can either specify a direction, or 2 to the left, or 7 to the right (it is also peak value by definition). Because it is only used to find one, so it is O (logn)

1  Public classSolution {2      Public intFindpeakelement (int[] num) {3         intL = 0;4         intr = Num.length-1;5          while(L <=r) {6             if(L = = r)returnl;7             intMid = (L + r)/2;8             if(Num[mid] < num[mid+1]) {9L = Mid + 1;Ten             } One             Else { AR =mid; -             } -         } the         return-1; -     } -}

Online another way to honestly compare the size of the elements after mid:

1  Public intFindpeakelement (int[] num) {2     intn =num.length;3     if(n <= 1)return0;4     //Handle the first and last element in num[]5     if(Num[0] > Num[1])return0;6     if(Num[n-1] > Num[n-2])returnN-1;7     intleft = 1, right = N-2;8      while(Left <=Right ) {9         intMid = (left + right) >> 1;Ten         if(Num[mid] > Num[mid-1] && num[mid] > num[mid + 1]) { One             returnmid; A}Else if(Num[mid] > num[mid + 1]) { -right = Mid-1; -}Else { theleft = mid + 1; -         } -     } -     return-1; +}

Leetcode:find Peak Element

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.