-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0162 [M] Find Peak Element
Code with Senpai edited this page Sep 15, 2022
·
21 revisions
T T T F
nums = [1,2,3,1]
assume [1,2,3,3] is an invalid testcase (we can use > instead of >=)
find the first F (leftmost) so [m] > [m+1]
class Solution:
def findPeakElement(self, A: List[int]) -> int:
n = len(A)
L = 0
R = n - 1 # -1 to check [m+1]
while L < R:
m = (L + R) // 2
down_slope = A[m] > A[m+1] # \
if not down_slope:
L = m + 1
else:
R = m
return L
footer