Skip to content

Commit 050caf5

Browse files
Time: 0 ms (100.00%) | Memory: 18 MB (38.72%) - LeetSync
1 parent 60640a2 commit 050caf5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import math
2+
3+
class Solution:
4+
def get(self, i):
5+
return -math.inf if i == -1 or i == len(self.nums) else self.nums[i]
6+
7+
def rising(self, i):
8+
return "left" if self.get(i) > self.get(i + 1) else "right"
9+
10+
def is_peak(self, i):
11+
return self.get(i - 1) < self.get(i) > self.get(i + 1)
12+
13+
def findPeakElement(self, nums: List[int]) -> int:
14+
l = 0
15+
r = len(nums) - 1
16+
17+
self.nums = nums
18+
19+
while l < r - 1:
20+
m = l + int((r - l) / 2)
21+
22+
if self.rising(m) == "right":
23+
l = m
24+
else:
25+
r = m
26+
27+
return l if self.is_peak(l) else r

0 commit comments

Comments
 (0)