Leetcode "Find Peak Element" Python implementation

Source: Internet
Author: User

title :

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.

Code :

The two methods are implemented by recursive and non-recursive method respectively.

Non-recursive code: OJ Test via runtime:55 ms

1 classSolution:2     #@param num, a list of integers3     #@return An integer4     deffindpeakelement (self, num):5         ifLen (num) = = 1 :6             return07         ifLen (num) = = 2 :8             return[0,1] [Num[0] < num[1]]9Start =0Tenend = Len (num)-1 One          whileStart <=End: A             ifStart = =End: -                 returnStart -             ifStart+1 = =End: the                 return[Start,end] [Num[start] <Num[end]] -Mid = (start+end)/2 -             ifNum[mid] < Num[mid-1]: -                 #start = 0 +End = Mid-1 -             elifNum[mid] < num[mid+1]: +Start = Mid+1 A                 #end = Len (num)-1 at             Else: -                 returnMid

Recursive code: OJ Test passed runtime:51 ms

1 classSolution:2     #@param num, a list of integers3     #@return An integer4     defFind (self,num,start,end):5         ifStart = =End:6             returnStart7         ifEnd = = Start + 1:8             ifNum[start] <Num[end]:9                 returnEndTen             Else: One                 returnStart AMid = (start+end)/2 -         ifNum[mid] < Num[mid-1] : -             returnSelf.find (num, start, mid-1) the         ifNum[mid] < num[mid+1] : -             returnself.find (num, Mid, end) -         returnMid -          +     deffindpeakelement (self, num): -         ifLen (num) = = 1: +             return0 A         returnSelf.find (num, 0, Len (num)-1)

Ideas :

Two points to find ideas upgrade version. Refer to the following two log ideas:

http://bookshadow.com/weblog/2014/12/06/leetcode-find-peak-element/

http://blog.csdn.net/u010367506/article/details/41943309

This problem in understanding the test instructions need to be aware that the default is the array of virtual and virtual tails are negative infinity, and the array does not exist equal two elements, so you will be able to find the peak point described in the title.

Leetcode "Find Peak Element" Python implementation

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.