Row With Maximum Ones - Problem
Given an m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.
In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.
Return an array containing the index of the row, and the number of ones in it.
Input & Output
Example 1 — Basic Case
$
Input:
mat = [[0,1,1,1],[0,0,1,1],[0,0,0,1]]
›
Output:
[0,3]
💡 Note:
Row 0 has 3 ones, Row 1 has 2 ones, Row 2 has 1 one. Maximum is 3 in row 0.
Example 2 — Tie Breaking
$
Input:
mat = [[0,0],[1,1],[0,0]]
›
Output:
[1,2]
💡 Note:
Only Row 1 has ones (2 ones), so return [1,2].
Example 3 — Multiple Ties
$
Input:
mat = [[0,1],[1,0],[0,1]]
›
Output:
[0,1]
💡 Note:
Rows 0, 1, and 2 all have 1 one each. Return smallest row index [0,1].
Constraints
- 1 ≤ m, n ≤ 100
- mat[i][j] is either 0 or 1
Visualization
Tap to expand
Understanding the Visualization
1
Input Matrix
Binary matrix with 0s and 1s
2
Count Ones
Count 1s in each row
3
Find Maximum
Return [row_index, count] of row with most 1s
Key Takeaway
🎯 Key Insight: Process rows sequentially to naturally handle ties by preferring smaller indices
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code