Search in a 2D Matrix (LC-0074)
class Solution:
def searchMatrix(self, matrix, target) -> bool:
def binsearch(A, lx, rx, target, nR, nC):
if lx <= rx:
mx = lx + (rx - lx) // 2
if A[mx // nC][mx % nC] > target:
return binsearch(A, 0, mx - 1, target, nR, nC)
elif A[mx // nC][mx % nC] < target:
return binsearch(A, mx + 1, rx, target, nR, nC)
else:
return True
return False
nR = len(matrix)
nC = len(matrix[0])
return binsearch(matrix, 0, nR * nC - 1, target, nR, nC)