Find the Row Containing the Most 1s in a Binary Matrix
Implement rowWithMax1s
Given a binary matrix where every row is sorted — all 0s before all 1s — return the index of the row with the most 1s. If every row has zero 1s, return
-1.
A row's 1-count is fully determined by where its first 1 sits, so binary-search each row for that index instead of scanning it, for an O(m log n) solution.
Example 1:
Input: matrix = [[0,0,1],[1,1,1]]
Output: 1
Example 2:
Input: matrix = [[0,0,0,1],[0,1,1,1],[1,1,1,1],[0,0,0,0]]
Output: 2
Example 3:
Input: matrix = [[0,0],[0,0]]
Output: -1
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ matrix.length, matrix[0].length ≤ 1000 - ●
Every value is 0 or 1 - ●
Every row is sorted — all 0s before all 1s
matrix =
[[0,0,1], [1,1,1]]