-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpeak.py
More file actions
29 lines (25 loc) · 693 Bytes
/
peak.py
File metadata and controls
29 lines (25 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Naive method
n = len(nums)
peak = None
if n > 2:
for i in range(1,n-1):
if nums[i] > nums[i-1] and nums[i] > nums[i+1]:
#print nums[i], i
peak = i
if not peak:
return nums.index(max(nums))
else:
return peak
elif n > 1:
if nums[1] > nums[0]:
return 1
else:
return 0
else:
return 0