-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_2d_matrix.py
33 lines (28 loc) · 1.04 KB
/
search_2d_matrix.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
"""
You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.
"""
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix:
return False
n = len(matrix[0]) # number of columns
m = len(matrix) # number of rows
def binarySearch(row):
l, r = 0, n-1
while l <= r:
mid = (l + r) // 2
if matrix[row][mid] == target:
return True
elif matrix[row][mid] < target:
l = mid + 1
else:
r = mid - 1
return False
for row in range(m):
if binarySearch(row):
return True
return False