class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# time O(Nlg(N)) N = cols*rows space O(1)
"""
main idea: convert 2D to 1D ,and mirrow 1D value to 2D by // col and % col for row_pos and col_pos.
"""
# edge case:
if not matrix or not matrix[0] or target < matrix[0][0] or target > matrix[-1][-1] :
return False
row = len(matrix)
col = len(matrix[0])
l,r = 0,row*col-1
while l <= r:
mid = l + (r-l)//2
i = mid//col
j = mid%col
if matrix[i][j]< target:
l = mid+1
elif matrix[i][j]> target:
r = mid-1
else:
return True