Skip to content

Commit e8813a6

Browse files
authored
Create find-a-peak-element-ii.py
1 parent f19816a commit e8813a6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python/find-a-peak-element-ii.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(min(n, m) * log(max(n, m)))
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def findPeakGrid(self, mat):
6+
"""
7+
:type mat: List[List[int]]
8+
:rtype: List[int]
9+
"""
10+
def get_vec(mat, i):
11+
return mat[i] if len(mat) < len(mat[0]) else (mat[j][i] for j in xrange(len(mat)))
12+
13+
def check(mat, x):
14+
return max(get_vec(mat, x)) > max(get_vec(mat, x+1))
15+
16+
left, right = 0, (min(len(mat), len(mat[0]))-1)-1
17+
while left <= right:
18+
mid = left + (right-left)//2
19+
if check(mat, mid):
20+
right = mid-1
21+
else:
22+
left = mid+1
23+
max_left = max(get_vec(mat, left))
24+
return [left, next(i for i, x in enumerate(get_vec(mat, left)) if x == max_left)][::1 if len(mat) < len(mat[0]) else -1]

0 commit comments

Comments
 (0)