LeetCode/Top Interview 150

BST: 74. Search a 2D Matrix

hyunkookim 2024. 12. 20. 20:18

74. Search a 2D Matrix

 

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        ROWS = len(matrix)
        COLS = len(matrix[0])

        l, r = 0, COLS*ROWS - 1

        while l<=r:
            mid = (l+r)//2

            rr = mid // COLS
            cc = mid % COLS

            if matrix[rr][cc] < target:
                l = mid+1
            elif matrix[rr][cc] > target:
                r = mid-1
            else:
                return True

        return False

 

https://youtu.be/Ber2pi2C0j0