Search a 2D Matrix
Write the code, see it run, pass the checks.
The problem
- medium
- Code
- Pro
Write searchMatrix(matrix, target): matrix is an array of rows, all the same length, with at least one row and one column. Each row is in ascending order, and each row's first number is greater than the previous row's last, so read row by row it's one sorted list. Return true if target is in it, and false if not. Don't change matrix. For example, in [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 3 is there and 13 isn't. The checks run 30,000 searches on half a million numbers, as 500 rows of 1,000 and as 1,000 rows of 500, so looking at every number is too slow.
Your code must pass
- Finds numbers anywhere in the matrix
- Returns false for missing numbers
- Handles one row, one column and one number
- Is fast enough for 30,000 searches