Find row with maximum 1's
Pattern: Binary search on 2D matrices
Each row is 0s followed by 1s, so the lower bound of 1 in a row tells you how many 1s it has. Do that for every row and keep the first row with the most.
The problem
You are given a matrix of 0s and 1s where every row is sorted, so each row is some 0s followed by some 1s. Return the
index of the row with the most 1s. If several rows tie, return the smallest index. If there are no 1s at all, return -1.
For example, [[1, 1, 1], [0, 0, 1], [0, 0, 0]] gives 0, and [[0, 0], [0, 0]] gives -1.
The function name rowWithMax1s matches the takeUforward practice page. Use the link above for the exact statement and
limits.
Think first
- In one sorted row of 0s and 1s, how do you count the 1s without looking at every cell?
- How do you handle ties so the smaller index wins?
- What should the answer start as, so that a matrix with no 1s returns -1?
Hints
Brute force, and why it fails
Count the 1s of each row by scanning: O(R × C). Binary search per row: O(R log C). A staircase from the top-right corner (move left on a 1, down on a 0) is O(R + C).
The key insight
A sorted row of 0s and 1s is fully described by where its 1s start. Finding that point is exactly a lower bound.
Dry run
| best | 0 |
|---|---|
| row | -1 |
mat has rows 1 1 1, 0 0 1 and 0 0 0. Each row is sorted, 0s first.
The plan
best = 0, row = -1
for i from 0 to R - 1:
ones = C - lowerBound(mat[i], 1)
if ones > best: best = ones; row = i
return row
Complexity
- Time
O(R log C)- Space
O(1)
One binary search per row.
Edge cases
- No 1s anywhere: -1.
- Ties, as in
[[0, 1, 1], [0, 1, 1]]: 0. - A row of all 1s: its lower bound is 0, so it has
Cones.
I've built my logic, show the code
Java
class Solution {
public int rowWithMax1s(int[][] mat) {
int best = 0, row = -1;
for (int i = 0; i < mat.length; i++) {
int ones = mat[i].length - firstOne(mat[i]); // the row is 0s then 1s
if (ones > best) { best = ones; row = i; } // strictly more, so ties keep the smaller index
}
return row;
}
private int firstOne(int[] r) { // lower bound of 1
int lo = 0, hi = r.length;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (r[mid] == 1) hi = mid;
else lo = mid + 1;
}
return lo;
}
}C++
class Solution {
public:
int rowWithMax1s(vector<vector<int>>& mat) {
int best = 0, row = -1;
for (size_t i = 0; i < mat.size(); i++) {
int ones = static_cast<int>(mat[i].size()) - firstOne(mat[i]); // the row is 0s then 1s
if (ones > best) { best = ones; row = static_cast<int>(i); } // ties keep the smaller index
}
return row;
}
int firstOne(vector<int>& r) { // lower bound of 1
int lo = 0, hi = static_cast<int>(r.size());
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (r[mid] == 1) hi = mid;
else lo = mid + 1;
}
return lo;
}
};Python
class Solution:
def rowWithMax1s(self, mat: list[list[int]]) -> int:
best, row = 0, -1
for i, r in enumerate(mat):
lo, hi = 0, len(r) # lower bound of 1: the row is 0s then 1s
while lo < hi:
mid = (lo + hi) // 2
if r[mid] == 1:
hi = mid
else:
lo = mid + 1
ones = len(r) - lo
if ones > best: # strictly more, so ties keep the smaller index
best, row = ones, i
return rowCommon mistakes
- Using
≥when comparing counts, which returns the last of tied rows. - Starting
rowat 0, which returns 0 for a matrix with no 1s. - Scanning whole rows when they are sorted.