Row With Maximum Ones

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.

Example 1
0 1
1 0
Inputmat = [[0,1],[1,0]]
Output[0,1]
Both rows have the same number of 1's, so we return the smaller row index 0 and the maximum count of ones 1.
Example 2
0 0 0
0 1 1
Inputmat = [[0,0,0],[0,1,1]]
Output[1,2]
The row indexed 1 has the maximum count of ones, which is 2, so the answer is [1,2].

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • mat[i][j] is either 0 or 1.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate