Skip to content

Commit

Permalink
O(logn) time and O(1) space using Binary Search(Divide and Conquer)
Browse files Browse the repository at this point in the history
  • Loading branch information
149ps committed Nov 15, 2019
1 parent 6a3e056 commit 6cd2800
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 162. Find Peak Element/162. Find Peak Element.py
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

0 comments on commit 6cd2800

Please sign in to comment.