Search a 2D Matrix II (Young Tableau)
Search a 2D Matrix II (Young Tableau) is a medium binary search problem solved with the 2d binary search + step search pattern.
The best approach, optimal (staircase / step search), runs in O(m + n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from binary search each row up.
Problem
In an m × n matrix (a Young tableau), every row is sorted left to right and every column is sorted top to bottom. Decide whether target is present.
Examples
Example 1
- Input
matrix = [[1,4,7],[2,5,8],[3,6,9]], target = 6- Output
true
Example 2
- Input
matrix = [[1,4,7],[2,5,8],[3,6,9]], target = 10- Output
false
Constraints
1 <= m, n <= 300- Each row is sorted left to right, each column top to bottom.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Binary search each row | O(m log n) | O(1) |
| Optimal (staircase / step search) | O(m + n) | O(1) |
1Binary search each row
O(m log n)O(1)Every row is sorted, so binary search each one.
- For each row, run binary search for target.
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
for (int[] row : matrix) {
int lo = 0, hi = row.length - 1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (row[mid] == target) return true;
if (row[mid] < target) lo = mid + 1; else hi = mid - 1;
}
}
return false;
}
}class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
for row in matrix:
lo, hi = 0, len(row) - 1
while lo <= hi:
mid = (lo + hi) // 2
if row[mid] == target:
return True
if row[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return Falseclass Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for (vector<int>& row : matrix) {
int lo = 0, hi = (int)row.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (row[mid] == target) return true;
if (row[mid] < target) lo = mid + 1; else hi = mid - 1;
}
}
return false;
}
};function searchMatrix(matrix, target) {
for (const row of matrix) {
let lo = 0, hi = row.length - 1;
while (lo <= hi) {
const mid = (lo + hi) >>> 1;
if (row[mid] === target) return true;
if (row[mid] < target) lo = mid + 1; else hi = mid - 1;
}
}
return false;
}2Optimal (staircase / step search)
O(m + n)O(1)Start at the top-right. Everything below it in its column is larger, and everything left of it in its row is smaller. If the value is too big, the whole column can go (move left); if too small, the whole row can go (move down).
- r = 0, c = n - 1.
- Equal: true. Greater: c--. Smaller: r++.
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r = 0, c = matrix[0].length - 1;
while (r < matrix.length && c >= 0) {
int v = matrix[r][c];
if (v == target) return true;
if (v > target) c--; else r++;
}
return false;
}
}class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
r, c = 0, len(matrix[0]) - 1
while r < len(matrix) and c >= 0:
v = matrix[r][c]
if v == target:
return True
if v > target:
c -= 1
else:
r += 1
return Falseclass Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int r = 0, c = (int)matrix[0].size() - 1;
while (r < (int)matrix.size() && c >= 0) {
int v = matrix[r][c];
if (v == target) return true;
if (v > target) c--; else r++;
}
return false;
}
};function searchMatrix(matrix, target) {
let r = 0, c = matrix[0].length - 1;
while (r < matrix.length && c >= 0) {
const v = matrix[r][c];
if (v === target) return true;
if (v > target) c--; else r++;
}
return false;
}Edge cases to test
- Target smaller than the top-left or larger than the bottom-right
- Single row or column
Hints
Hint 1
At the top-right corner, one move eliminates a whole row or a whole column.
FAQ
What is the best time complexity for Search a 2D Matrix II (Young Tableau)?
Optimal (staircase / step search) runs in O(m + n) time and O(1) extra space.
Which pattern does Search a 2D Matrix II (Young Tableau) use?
It is a binary search problem that uses the 2d binary search + step search pattern. Other problems with the same pattern: Row with max 1s, Search a 2D Matrix, Leftmost Column with at Least a One.
Is there a brute force solution for Search a 2D Matrix II (Young Tableau)?
Yes. Binary search each row takes O(m log n) time and O(1) space. Every row is sorted, so binary search each one.
Which edge cases should I test for Search a 2D Matrix II (Young Tableau)?
Target smaller than the top-left or larger than the bottom-right; Single row or column.