Medium · Matrices

Search a 2D Matrix II

Given an m × n integer matrix whose rows are sorted ascending left to right and whose columns are sorted ascending top to bottom, and an integer target, return true if target occurs in the matrix, otherwise false.

Examples

Example 1

{
  "matrix": [
    [1, 4, 7, 11],
    [2, 5, 8, 12],
    [3, 6, 9, 16],
    [10, 13, 14, 17]
  ],
  "target": 5
}

Output: true (found)

Example 2

{
  "matrix": [
    [1, 4, 7, 11],
    [2, 5, 8, 12],
    [3, 6, 9, 16],
    [10, 13, 14, 17]
  ],
  "target": 15
}

Output: false (not found)

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems