Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Method 1:
- class Solution:
- def findPeakElement(self, a: List[int]) -> int:
- n=len(a)
- l,r=0,n-1
- while l<r:
- m=(l+r)>>1#Find mid
- '''
- You found increasing part from mid right side. So 100% sure that peak will be rightside.
- It is not 100% sure that peak will be left. So you shift l to m+1
- '''
- if a[m]<a[m+1]:
- l=m+1
- '''
- You found decreasing part from mid to left side. So 100% sure that peak will be left side.
- It is not 100% sure peak will be right. So you shift r to m. Since even r is eligible for being Peak(VVimp)
- '''
- else:
- r=m
- '''
- To avoid infinite loop we removed l<=r and made it to l<r
- '''
- return l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement