Advertisement
smj007

find peak element

Aug 17th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. class Solution:
  2.     def findPeakElement(self, nums: List[int]) -> int:
  3.         if len(nums) <= 1:
  4.             return 0
  5.  
  6.         low, high = 0, len(nums) - 1
  7.    
  8.         while low <= high:
  9.             mid = low + (high-low)//2
  10.    
  11.             if mid < len(nums) - 1 and nums[mid] < nums[mid+1]:
  12.                 low = mid + 1
  13.             elif mid > 0 and nums[mid] < nums[mid-1]:
  14.                 high = mid - 1
  15.             else:
  16.                 return mid
  17.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement