-
Notifications
You must be signed in to change notification settings - Fork 2
/
852.py
51 lines (44 loc) · 1.34 KB
/
852.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# [ LeetCode ] 852. Peak Index in a Mountain Array
def solution(arr: list[int]) -> int:
start, end = 0, len(arr) - 1
while start <= end:
middle: int = (start + end) // 2
target, previous, next = arr[middle], arr[middle-1], arr[middle+1]
if target < previous:
end = middle
elif target < next:
start = middle
else:
return middle
def another_solution(arr: list[int]) -> int:
start, end = 0, len(arr) - 1
while start < end:
middle: int = (start + end) // 2
target, next_value = arr[middle], arr[middle+1]
if target < next_value:
start = middle + 1
else:
end = middle
return start
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[int]] | int]] = [
{
"input": { "arr": [0, 1, 0] },
"output": 1
},
{
"input": { "arr": [0, 2, 1, 0] },
"output": 1
},
{
"input": { "arr": [0, 10, 5, 2] },
"output": 1
},
{
"input": { "arr": [3, 9, 8, 6, 4] },
"output": 1
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])