Search a 2D Matrix II

medium

You are given an m x n grid of integers matrix with a double-sorted structure:

  • every row is sorted in ascending order, left to right,
  • every column is sorted in ascending order, top to bottom.

Given an integer target, return true if target appears anywhere in the grid, and false otherwise.

Note what the guarantee does not say: the grid is not one long sorted list — the last value of a row may be larger than the first value of the next row. An efficient search must exploit both sort orders instead of touching every cell.

Hints

Every row is sorted and every column is sorted — but the grid is not one long sorted list. Is there a cell where a single comparison rules out many cells at once?
Look at the four corners. At the top-left, moving right and moving down both increase the value, so a comparison tells you nothing. Is there a corner where the two directions pull opposite ways?
Start at the top-right. If that value is bigger than target, its entire column is bigger — step left. If smaller, its entire row is smaller — step down. You finish in at most m + n steps.

Common doubts

Because the rows overlap: the last value of one row is not guaranteed to be smaller than the first value of the next (row 0 ends at 15 while row 1 starts at 2 in the example). Without that chain there is no single sorted order to binary search. That trick only works on a fully sorted matrix.
Both available moves — right and down — increase the value, so when the current cell is smaller than target you have no idea which way to go. Only the top-right and bottom-left corners give a decisive move for both outcomes.
Yes — sorted does not mean distinct here. The staircase handles duplicates for free because each step only asks whether the current value is bigger, smaller, or equal to target.

Interview follow-ups

The exact same staircase walk: start at a corner and, instead of stopping at equality, accumulate how many cells in the eliminated row or column are negative. Still O(m + n).
Binary search on the value range, and for each candidate value use the staircase walk to count how many cells are less than or equal to it — O((m + n) log(range)). That is Kth Smallest Element in a Sorted Matrix.
Binary searching each row streams nicely because it touches rows independently and in order, while the staircase needs random jumps between rows. Access pattern can beat asymptotic complexity once I/O dominates.

Fun facts

  • The staircase technique is known as saddleback search — the top-right cell sits like a saddle point, the maximum of its row and the minimum of its column.
  • A grid sorted along both axes is a Young tableau, a structure from combinatorics; inserting into one and extracting its minimum are classic exercises in their own right.
  • The eliminate-a-line-per-comparison walk reappears in Count Negative Numbers in a Sorted Matrix and inside the counting step of Kth Smallest Element in a Sorted Matrix.

Asked at

AmazonMicrosoftGoogleAppleAdobe
Frequently Sometimes Occasionally
Example 1
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true
5 sits at row 1, column 1 — it is in the grid.
Example 2
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false
20 falls between values that exist (19 and 21) but never appears itself.
Constraints

- m == matrix.length - n == matrix[i].length - 1 <= n, m <= 300 - -10^9 <= matrix[i][j] <= 10^9 - All integers in each row are sorted in ascending order, left to right - All integers in each column are sorted in ascending order, top to bottom - -10^9 <= target <= 10^9

Solve this problem →