Skip to content

Commit 8310d17

Browse files
Peak Index Binary Search Solution 2
1 parent 78bd9f2 commit 8310d17

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

0852-Peak-Index-In-A-Mountain-Array.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ def peakIndexInMountainArray(self, arr: List[int]) -> int:
3535
end = mid
3636
else:
3737
start = mid + 1
38-
return start
38+
return start
39+
40+
# Method 2
41+
42+
class Solution:
43+
def peakIndexInMountainArray(self, arr: List[int]) -> int:
44+
start, end = 0, len(arr) - 1
45+
while start <= end:
46+
mid = (start + end) // 2
47+
if arr[mid - 1] < arr[mid] > arr[mid + 1]:
48+
return mid
49+
elif arr[mid - 1] < arr[mid]:
50+
start = mid
51+
else:
52+
end = mid

0 commit comments

Comments
 (0)