-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0162.py
More file actions
29 lines (23 loc) · 682 Bytes
/
q0162.py
File metadata and controls
29 lines (23 loc) · 682 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
#!/usr/bin/python3
from typing import List
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
right = len(nums) - 1
if right == 0 or nums[0] > nums[1]:
return 0
if nums[right] > nums[right - 1]:
return right
left = 1
right -= 1
while left <= right:
mid = (left + right) >> 1
if nums[mid] > nums[mid - 1]:
if nums[mid] > nums[mid + 1]:
return mid
left = mid + 1
else:
right = mid - 1
return -1
nums = [1, 2, 1, 2, 1]
result = Solution().findPeakElement(nums)
print(result)