74. Search a 2D Matrix
You are given an m x n integer matrix matrix with the following two properties:
- Each row is sorted in non-decreasing order.
- The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.
Example 1:
Example 2:
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 100
- -104 <= matrix[i][j], target <= 104
Constraints:
Notes
Intuition
The matrix is sorted both row-wise and column-wise, with each row's first element greater than the previous row's last. First binary search to find the correct row, then binary search within that row.
Implementation
Use binary search on rows: a row is valid if target ≥ row[0] and target ≤ row[-1]. Once the correct row is found, perform standard binary search within it. If the target isn't in the valid row, return False immediately.
Edge-cases
If the row-level binary search completes without finding a valid row, return False—don't proceed to the column search.
- Time: O(log m + log n) — binary search on rows then columns
- Space: O(1) — only tracking pointers