Leetcode OJ 162. 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[i+1] , the find a peak element and return its index.

The array may be contain multiple peaks, in this case return the index to any one of the peaks is fine.

May imagine num[-1] = num[n] = -∞ .

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

Click to show spoilers.

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

"Problem Analysis"

Any two adjacent elements in an array are not the same, and find such an element, which is larger than its neighbor nodes. If such an element exists, return any one of them.

"The idea of disintegration"

Method 1:

Iterate through the array,

    • If it is the leftmost element, and it is larger than the right neighbor, it is the peak elmemnt.
    • If it is the rightmost element, and it is larger than the left neighbor, it is also the peak element.
    • Otherwise, if it is larger than the left and right neighbors, it is also the peak element.

The code is as follows:

1 classSolution2 {3  Public:4     intFindpeakelement (vector<int>&nums)5     {6         if(nums.size () = = 1)7         {8             return0;9         }Ten  One          for(inti = 0; I < nums.size (); i++) A         { -             if((i = = 0 && nums[i] > nums[i + 1]) -|| (i = = Nums.size ()-1 && nums[i] > Nums[i-1])) the             { -                 returni; -             } -  +             if(0 < I && i < nums.size ()-1 -&& Nums[i] > Nums[i-1] && nums[i] > nums[i + 1]) +             { A                 returni; at             } -         } -  -         return-1; -     } -};
Method 2:

The above algorithm uses a lot of comparisons, the following gives a simpler code:

1 classSolution2 {3  Public:4     intFindpeakelement (vector<int>&nums)5     {6         //for Nums[0]: the title specifies that it is larger than the left neighbor (does not exist), so as long as it is larger than the right neighbor, it is the peak element. 7         //if not, that means it is no bigger than the right neighbor. The topic specifies that adjacent nodes are not equal, so it is necessarily smaller than the right neighbor. 8         // 9         //for Nums[1]: The above analysis shows that the left neighbor is less than the current node. So as long as it's bigger than the right neighbor, it's the peak element. Ten         //if not, that means it is also smaller than the right neighbor.  One         // ... A         //for nums[sz-2]: If none have been returned before. From the above rule: it is larger than the left neighbor.  -         //and according to the title, it must be greater than the right neighbor (does not exist). So it must be the peak element.  -          for(inti = 0; I < Nums.size ()-1; i++) the         { -             if(Nums[i] > nums[i + 1]) -             { -                 returni; +             } -         } +         returnNums.size ()-1; A     } at};

The above two solutions can be leetcode accepted, but their time complexity is O (n). In the title, the request is:O (log n).

Method 3:

From the above analysis, we can draw such a rule:

As long as the array satisfies the following form:

[ 小,大,... 未知 ...,大,小 ]

Then it's sure to find peak elememnt.

Try to 二分法 process with:

The middle value of the array mid ,

    • If mid the value is mid+1 smaller than the value

      Then from mid the beginning the right half of the array will satisfy: [小,大,...,大,小] the form, so there must be a peak element.

      And because mid it's definitely not the peak element, you can further narrow it down to [mid+1, 数组末尾] .

    • If mid the value mid+1 is greater than the value

      Then from the array 开始位置 to the mid+1 position, will also constitute [小,大,...,大,小] the form.

      And mid+1 obviously not the peak element. So the scope can be narrowed down further [数组开始,mid] .

The code is as follows:

1  Public classSolution {2      Public intFindpeakelement (int[] nums) {3         intleft = 0, right = nums.length-1;4          while(Left <Right ) {5             intMid = (Right-left)/2 +Left ;6             if(Nums[mid] > nums[mid+1])7right =mid;8             Else 9left = mid + 1;Ten         } One         returnLeft ; A     } -}

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