Skip to content
Decoder

Search a 2D Matrix II

Medium

Problem

You are given an m x n grid matrix where each row is sorted left to right and each column is sorted top to bottom. Unlike Search a 2D Matrix, a row can start below where the previous row ended, so the grid is not one sorted list. Return true if target is in the grid.

Example 1

matrix = [[1,  4,  7,  11, 15],

Example 2

          [2,  5,  8,  12, 19],

Example 3

          [3,  6,  9,  16, 22],

Example 4

          [10, 13, 14, 17, 24],

Example 5

          [18, 21, 23, 26, 30]]

Example 6

Input
target = 5
Output
true

Example 7

Input
target = 20
Output
false

Constraints

  • 1 <= m, n <= 300
  • -10^9 <= matrix[i][j], target <= 10^9
  • Every row and every column is sorted in ascending order.

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.