Input: mat =[[0,1],[1,0]]Output: [0,1]Explanation: Both rows have the same number of 1's. So we return the index of the smaller row,0, and the maximum count of ones(1`)`. So, the answer is[0,1].
Example 2:
1
2
3
Input: mat =[[0,0,0],[0,1,1]]Output: [1,2]Explanation: The row indexed 1 has the maximum count of ones `(2)`. So we return its index,`1`, and the count. So, the answer is[1,2].
Example 3:
1
2
3
Input: mat =[[0,0],[1,1],[0,0]]Output: [1,2]Explanation: The row indexed 1 has the maximum count of ones(2). So the answer is[1,2].
The naive approach to solve this involves iterating through each row in the matrix, counting the number of 1s in each row, and keeping track of the row with the highest count. We can use a simple comparison to record the maximum count and the corresponding row index.
classSolution:
def row_with_max_ones(self, mat: List[List[int]]) -> List[int]:
max_ones = 0
ans =[-1, 0]for i, row in enumerate(mat):
count_ones = row.count(1)
if count_ones > max_ones:
max_ones = count_ones
ans =[i, count_ones] elif count_ones == max_ones and ans[0]==-1:
ans =[i, count_ones]return ans