Find Peak Element (Binary Search)

Source: Internet
Author: User

Find Peak Element (Binary Search)

A peak element is an element that is greater than its neighbors.

Given an input array where num [I] =num [I + 1], find a peak element and return its index.

The array may contain in multiple peaks, in that case return the index to anyone of the peaks is fine.

You may imagine that num [-1] = num [n] =-∞.

For example, in array [1, 2, 3, 1], 3 is apeak element and your function shocould return the index number 2.

Click to showspoilers.

Note:

Your solution shocould be in logarithmic complexity.

Credits:
Special thanks to @ ts for adding this problem and creating all testcases.

HideTags

Array Binary Search

 

 

Note:

0 ......... Media1, media2 ........ N-1

The interval of the second part is: 0 ......... Media1, media2, media1, media2 ........ N-1

If it is divided into 0 ......... Media and media ........ N-1

The media element is ignored.

In addition, overflow should be considered when adding negative infinity to the beginning and end to construct a new array.

In addition:Num. Push_back (-2147483647-1); // you cannot directly push-2147483648. If-2147483648 is automatically recognized as unsigned, you cannot apply the-number to the unsigned type.

 

 

 

 

# Pragma once # include
 
  
# Include
  
   
Using namespace std; // take struct int max (int a, int B) {if (a> B) return a; return B;} int findIt (long * list, int begain, int end) {if (end-begain + 1 <= 6) // to this extent, the traversal check is used as the recursive exit {for (int I = begain; I <= end-2; I ++) // note that the end-2if (list [I]> list [I + 1]) continue; elseif (list [I + 1]> list [I + 2]) return I; // note that the return parameter is not I + 1, because the lower mark in the list is greater than 1 elsecontinue in num; return-1; // for the end of the loop, return-1} int media1 = (begain + end)/2; int media2 = media1 + 1; // The subscript int aheadmedia = (begain + media2)/2 of the two elements in the middle; // The subscript int behindmedia = (media1 + end)/2 of the intermediate element in the first half; // subscripts of the middle element in the second half if (list [aheadmedia]> list [begain] & list [aheadmedia]> list [media2]) // The first half must have return findIt (list, begain, media2 ); else if (list [behindmedia]> list [media1] & list [behindmedia]> list [end]) // The first half does not exist. The second half must have return findIt (list, media1, end); else // may not exist before and after, but there must be one. The returned value is large. If not,-1 return max (findIt (list, begain, media2), findIt (list, media1, end);} int findPeakElement (const vector
   
    
& Num) {long * list = new long [num. size () + 2]; // list [0] = num [0]-1; // The simulation header is negative infinity // note, when num [0] is-2147483648, the preceding listing [0] will change to 2147483647, because num is int type and will overflow. Write as follows. List [0] = num [0]; list [0] --; // note !!!!!!! If list is int minus 1, int may overflow, so list should be of the long TYPE !!!! Long is also 4 in the 32-bit compiler, so long longlist [num. size () + 1] = num [num. size ()-1]; list [num. size () + 1] --; // The simulated header is negative infinity for (int I = 0; I <(int) num. size (); I ++) list [I + 1] = num [I]; // at this point, the new array with the positive beginning and end is constructed and return findIt (list, 0, num. size () + 1);} void main () {vector
    
     
Num; num. push_back (-2147483647-1); // you cannot directly push-2147483648. In this case,-2147483648 is automatically recognized as unsigned, you cannot apply The-sign to The unsigned cout <"The peak element's index in num is" <findPeakElement (num) <endl; system ("pause ");}
    
   
  
 


 

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.