Skip to content
BASA DSA

Search a 2D Matrix

MediumBinary Search

Return whether target occurs in a rectangular matrix whose rows increase and whose next row begins after the previous row ends. Use logarithmic time in the number of cells.

Example 1

Input
matrix = [[1,4,8],[10,13,20]]
target = 13
Output
true

Explanation

13 occurs at row 1, column 1.

Example 2

Input
matrix = [[1,4,8],[10,13,20]]
target = 9
Output
false

Explanation

9 lies between the two rows' ranges but is absent.

Example 3

Input
matrix = [[7]]
target = 7
Output
true

Explanation

The only cell equals the target.

Constraints

  • 1 <= rows, columns <= 100
  • All cells are integers in strictly increasing flattened order

Function contractsearchMatrixArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…