-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path74.py
40 lines (33 loc) · 883 Bytes
/
74.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
def searchMatrix(matrix: list[list[int]], target: int) -> bool:
# for row in matrix:
# L = 0
# R = len(row) - 1
# while L <= R:
# M = (L + R) // 2
# if row[M] == target:
# return True
# elif row[M] > target:
# R = M - 1
# else:
# L = M + 1
#
# return False
# Time complexity is O(M.logN)
m = len(matrix)
n = len(matrix[0])
t = m*n
L = 0
R = t - 1
while L <= R:
M = (L+R) // 2
i = M // n
j = M % n
mid_num = matrix[i][j]
if mid_num == target:
return True
elif mid_num > target:
R = M - 1
else:
L = M + 1
return False
print(searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 3))