Link: 74. Search a 2D Matrix
Track: Neetcode 150

Question

Restate the problem

Edge Case


Method 1
Method 2

Method

Approach

(discussed at lease two approach?)

Complexity

  • Time Complexity:
  • Space Complexity:

Code

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        rows = len(matrix)
        cols = len(matrix[0])
        left = 0
        right = rows * cols - 1
 
        while left <= right:
            mid = (left + right) // 2
            row = mid // cols # 7 // 5 = 1
            col = mid % cols # 7% 5 = 2
            val = matrix[row][col]
 
            if val == target:
                return True
            elif val < target:
                left = mid + 1
            else:
                right = mid - 1 
            
        return False

History

  • Jan-xx-2026 Peeked