Search a Matrix Sorted by Rows and Columns
Company: ByteDance
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Search a row-and-column sorted matrix with staircase elimination, proving why each move removes a safe region in linear rows-plus-columns time.
Constraints
- All rows have the same length. The row count, column count, and total number of cells each fit a signed 32-bit length.
- The rows and columns are independently sorted in nondecreasing order.
- Matrix values and the target are integers from -(2^53 - 1) to 2^53 - 1, inclusive.
Examples
Input: ([[1, 5, 9], [3, 7, 12], [6, 10, 16]], 7)
Expected Output: True
Explanation: The middle cell contains 7, even though a row can start below the end of the previous row.
Input: ([[0, 2, 7], [1, 3, 9], [4, 6, 12]], 8)
Expected Output: False
Explanation: The target lies between two matrix values and does not occur.
Hints
- Try tracing a comparison in a matrix where the first value in the second row is smaller than the last value in the first row.