Search a 2D Matrix II

Write an efficient algorithm that searches for a target value in an m x n integer matrix. Each row in the matrix is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= n, m <= 300
  • -10^9 <= matrix[i][j] <= 10^9
  • All the integers in each row are sorted in ascending order.
  • All the integers in each column are sorted in ascending order.
  • -10^9 <= target <= 10^9
Saving… · 2 lines
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
Console

Click Run to execute sample tests.