Search a 2D Matrix

Binary Search, problem 3 of 7

Search a 2D Matrix

Medium

LC #74

binary searchmatrix

Not attempted yet

You're given an m x n integer matrix where:

  • each row is sorted ascending, 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, else False. Aim for O(log(m * n)) time.

Example 1

Input: matrix = [[1,3,5,7],
                 [10,11,16,20],
                 [23,30,34,60]]
       target = 3
Output: True

Example 2

Input: matrix = [[1,3,5,7],
                 [10,11,16,20],
                 [23,30,34,60]]
       target = 13
Output: False

Example 3

Input: matrix = [[1]], target = 1
Output: True

Constraints

  • 1 <= m, n <= 300
  • -10^4 <= matrix[i][j], target <= 10^6

Python

Loading draft…

Test results

10 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 7 hidden

Run examples, then submit all tests.