Search a 2D Matrix
Given an m x n integer matrix where every row is sorted left to right and the first integer of each row is greater than the last integer of the previous row, decide whether a target value appears in the matrix. Return true if it does, otherwise false.
Open official problem prompt ↗Report whether a target integer exists anywhere in a matrix that is sorted as if its rows were laid end to end.
Think of a multi-page dictionary where every page continues alphabetically from the previous one. You can binary-search a global page-and-line position instead of scanning page by page.
- Input
- matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
- Output
- true
- Why
- 3 sits in the first row at column 1, so the target is present.
m == matrix.lengthn == matrix[i].length1 <= m, n <= 100-10^4 <= matrix[i][j], target <= 10^4