-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
O(logn) time and O(1) space using Binary Search(Divide and Conquer)
- Loading branch information
Showing
1 changed file
with
13 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def findPeakElement(self, nums: List[int]) -> int: | ||
lo = 0 | ||
hi = len(nums)-1 | ||
while lo < hi: | ||
mid = lo + (hi-lo)//2 | ||
if nums[mid] > nums[mid+1] and nums[mid] > nums[mid-1]: | ||
return mid | ||
elif nums[mid] < nums[mid+1]: | ||
lo = mid+1 | ||
elif nums[mid] < nums[mid-1]: | ||
hi = mid-1 | ||
return lo |