Skip to content
Decoder

Search a 2D Matrix

Medium

Problem

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.

Example 1

matrix = [[1, 3, 5, 7],

Example 2

          [10, 11, 16, 20],

Example 3

          [23, 30, 34, 60]]

Example 4

Input
target = 3
Output
true

Example 5

Input
target = 13
Output
false

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.