74. Search a 2D Matrix

Posted on Oct 20, 2023
# O(log(m * n)) time || O(1) space
def search_matrix(self, matrix: List[List[int]], target: int) -> bool:
    rows, cols = len(matrix), len(matrix[0])
    low, high = 0, rows * cols - 1
    while low <= high:
        mid = low + (high - low) // 2
        mid_row, mid_col = divmod(mid, cols)

        if target == matrix[mid_row][mid_col]:
            return True
        elif target < matrix[mid_row][mid_col]:
            high = mid - 1
        else:
            low = mid + 1

    return False

to get midpoint in the matrix, we use the divmod function with mid and cols