Skip to content

Search a 2D Matrix

MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

You are given an m x n grid of integers matrix with two properties: each row is sorted in ascending order, and the first value of each row is greater than the last value of the row above. Return true if target is in the matrix and false otherwise, in O(log(m · n)) time.

matrix = [[1, 3, 5, 7],
          [10, 11, 16, 20],
          [23, 30, 34, 60]]
 
target = 3    →  true
target = 13   →  false

Constraints

  • m == matrix.length, n == matrix[i].length
  • 1 <= m, n <= 100
  • -10^4 <= matrix[i][j], target <= 10^4