Find a Value in a Matrix Sorted Like a Flattened Array

Implement searchMatrix

You're given an m × n matrix where every row is sorted in ascending order, and the first number of each row is greater than the last number of the previous row — so reading it row by row produces one fully sorted sequence. Given an integer target, return whether it exists anywhere in the matrix. Solve it in O(log(m × n)) time by binary-searching the matrix as if it were a single flattened, sorted array — no need to actually flatten it.

Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3

Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13

Output: false

Example 3:

Input: matrix = [[1]], target = 1

Output: true

+ 4 hidden test cases run on Submit.

Constraints:

  • 1 ≤ matrix.length, matrix[0].length ≤ 100
  • -10⁴ ≤ matrix[i][j], target ≤ 10⁴
  • Every row is sorted in ascending order
  • The first integer of each row is greater than the last integer of the previous row

matrix =

[[1,3,5,7], [10,11,16,20], [23,30,34,60]]

target =

3