mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
Create find_peak_element.py
This commit is contained in:
parent
d0bbfed0e5
commit
06f3c4dd1e
1 changed files with 21 additions and 0 deletions
21
searching/find_peak_element.py
Normal file
21
searching/find_peak_element.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
# A peak element is an element that is strictly greater than its neighbors.
|
||||
|
||||
def peak_element(nums):
|
||||
|
||||
left, right = 0, len(nums) - 1
|
||||
|
||||
while left < right:
|
||||
|
||||
mid = (left + right) // 2
|
||||
|
||||
if nums[mid] > nums[mid + 1]:
|
||||
right = mid
|
||||
else:
|
||||
left = mid + 1
|
||||
|
||||
return left
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue