-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path162.py
More file actions
26 lines (21 loc) · 659 Bytes
/
Copy path162.py
File metadata and controls
26 lines (21 loc) · 659 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
class Solution:
def isPeak(self, nums, i):
if i == 0 and nums[i] > nums[i+1]:
return True
if i == len(nums)-1 and nums[i] > nums[i-1]:
return True
return nums[i-1] < nums[i] > nums[i+1]
def findPeakElement(self, nums: List[int]) -> int:
low = 0
high = len(nums)-1
if len(nums) == 1:
return 0
while low <= high:
mid = low + (high-low)//2
if self.isPeak(nums, mid):
return mid
if nums[mid+1] > nums[mid]:
low = mid + 1
else:
high = mid - 1
return -1