Leetcode:find Peak Element-looking for a vertex within an array

Source: Internet
Author: User

1. Title

Find Peak Element (looking for a vertex within an array)

2. Address of the topic

https://leetcode.com/problems/find-peak-element/

3. Topic content

English:

A peak element is an element, which 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 be contain multiple peaks, in this case return the index to any one of the peaks is fine.

Imagine that num[-1] = num[n] =-∞.

Chinese:

Within an array, an element is called a "vertex" if it is larger than the two elements around it. Now give an array of the adjacent elements that must be different, find a vertex element and return its index. An array may contain multiple vertices, in which case the coordinates of any one vertex are returned. You can assume that the 1 and nth elements are negative infinity (the meaning of this sentence is that the leftmost element assumes it has a smaller element than its left, and the rightmost element is the same).

For example: Given an array [1, 2, 3, 1], element 3 is a vertex and should return its index value 2

4. Method of solving Problems 1

The easiest way to think of it is to iterate the array from left to right, and if an element is larger than its left and right elements, the element must be a vertex. Returns the first vertex found.

The Java code is as follows:

/** * @ function Description: leetcode 162 - find peak element * @ Developer: Tsybius2014  * @ Development Time: November 4, 2015  */public class Solution {         /**     *  Find vertices      *  @param  nums     *  @return      */     Public int findpeakelement (int[] nums)  {                 if  (nums == null)  {             return -1;         } else if  (nums.length == 0)  {             return -1;        } else  if  (nums.length ==&NBSP;1)  {            return 0;         } else if  (nums[0] > nums[1])  {             return 0;         } else if  (nums[nums.length - 1] > nums[ NUMS.LENGTH&NBSP;-&NBSP;2])  {             return nums.length - 1;        }                 for  (int i =  1; i < nums.length - 1; i++)  {             if  (nums[i - 1] <= nums[i] &&  nums[i + 1] <= Nums[i])  {                 return i;            }         }                 return -1;    }}

4. Method of solving Problems 2

Another approach is to use the binary lookup method to solve the problem. This method takes advantage of the following properties in the topic:

1) The leftmost element, it's "more left" element is smaller than it (negative infinity), and we think it's a growing direction

2) The rightmost element, which has a "more right" element smaller than it (also negative infinity), we think it is a descending direction

According to these two points we can judge: the leftmost and rightmost elements surround the area, there must be at least one vertex

Now we find the midpoint Nums[mid], compare it to Nums[mid + 1], if the former is smaller, the direction is growth, and the leftmost element is the same, move the left edge to the position of the mid+1, otherwise, the right edge is moved to the position of the mid with the rightmost element.

The principle of this method is that when the left boundary direction is "growth" and the right-hand direction is "down", the area bounded by the two must have a vertex. We can write the following Java code to implement this method:

/** * @ function Description: leetcode 162 - find peak element * @ Developer: Tsybius2014  * @ Development Time: November 4, 2015  */public class Solution {         /**     *  Find vertices      *  @param  nums     *  @return      */     Public int findpeakelement (int[] nums)  {                 if  (nums == null)  {             return -1;         } else if  (nums.length == 0)  {             return -1;        } else  if  (nums.length ==&NBSP;1)  {            return 0;         }         int left  = 0;        int mid = 0;         int right = nums.length - 1;                 while  (Left < right)  {            mid = left +   (Right - left)  / 2;             if  (nums[mid] < nums[mid + 1])  {                 left = mid + 1;             } else {                 right = mid;             }        }                 return left;    }}

The subject can also be used recursively to search for binary points.

END

Leetcode:find Peak Element-looking for a vertex within an array

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.