Watch 10 video solutions for Search a 2D Matrix, a medium level problem involving Array, Binary Search, Matrix. This walkthrough by NeetCode has 261,802 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an m x n integer matrix matrix with the following two properties:
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:
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
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 100-104 <= matrix[i][j], target <= 104Problem Overview: You are given an m x n matrix where each row is sorted in ascending order and the first element of each row is greater than the last element of the previous row. The task is to determine whether a given target value exists in the matrix.
Approach 1: Binary Search on Matrix as a 1D Array (O(log(m*n)) time, O(1) space)
The key insight is that the matrix behaves like a globally sorted array. Because every row starts with a value greater than the previous row’s last value, you can conceptually flatten the matrix into a single sorted list of size m * n. Run standard binary search on indices from 0 to m*n - 1. Convert a 1D index back to matrix coordinates using row = mid / n and col = mid % n. Each step compares matrix[row][col] with the target and halves the search space. This approach is optimal because it treats the structure as a single sorted dataset while keeping constant memory.
Approach 2: Two-step Binary Search (Row then Column) (O(log m + log n) time, O(1) space)
This method performs two separate binary searches. First, run binary search on the rows to locate the only row that could contain the target. Compare the target with the first and last element of each row to narrow down the candidate row. Once the correct row is identified, run a second binary search within that row to locate the target value. This approach explicitly uses the row ordering property of the matrix and the sorted order inside each row. It is easy to reason about during interviews because the two steps clearly mirror the structure of the data.
Both solutions rely on the same property: the matrix behaves like a sorted structure. The first approach compresses the entire matrix into a conceptual array, while the second approach searches rows and columns separately. Either way, the algorithm repeatedly halves the search space using array indexing and binary search comparisons.
Recommended for interviews: The 1D binary search approach is usually the expected optimal solution. It demonstrates that you recognize the matrix is globally sorted and can transform the problem into classic binary search. The two-step method is also accepted and often easier to derive during an interview because it directly uses the matrix layout.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Binary Search on Matrix as 1D Array | O(log(m*n)) | O(1) | Best general solution when the matrix behaves like a globally sorted array |
| Two-step Binary Search (Row then Column) | O(log m + log n) | O(1) | Useful when reasoning about row ranges first and then searching within a row |