We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 60640a2 commit 050caf5Copy full SHA for 050caf5
162-find-peak-element/find-peak-element.py
@@ -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