All files / leetCode 0074.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23        2x 2x 2x 2x   2x 7x 7x 7x 7x 6x   2x 4x     1x    
export default function searchMatrix(
  matrix: number[][],
  target: number,
): boolean {
  const m = matrix.length
  const n = matrix[0].length
  let lo = 0
  let hi = m * n - 1
 
  while (lo <= hi) {
    const mid = lo + ((hi - lo) >> 1)
    const row = Math.floor(mid / n)
    const col = mid % n
    if (matrix[row][col] === target)
      return true
    else if (matrix[row][col] < target)
      lo = mid + 1
    else hi = mid - 1
  }
 
  return false
}