Show problems as
Classic problem #2643EasyArrayMatrix

Row With Maximum Ones

Real-world version: Busiest Row Of The Punch Sheet
Tracked in this browser only
Read Row With Maximum Ones on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A punch sheet is recorded as a grid sheet of 0s and 1s, where sheet[r][c] is 1 when the cell in row r and column c has been punched and 0 when it has not. Every row of the sheet has the same number of columns.

Find the row that holds the most punched cells. If several rows hold the same number of punched cells, and that is the most, take the one with the smallest row index.

Return a two-entry array [row, punches], where row is the index of that row and punches is how many punched cells it holds.

Examples

Example 1

Input
sheet = [[0, 1, 1, 0], [1, 0, 0, 0], [0, 1, 1, 1]]
Output
[2, 3]

The rows hold 2, 1 and 3 punched cells, so row 2 with its 3 punches is reported.

Example 2

Input
sheet = [[1, 0, 1], [0, 1, 0], [1, 1, 0]]
Output
[0, 2]

Rows 0 and 2 both hold 2 punched cells and row 1 holds 1. The tie between rows 0 and 2 goes to the smaller index.

Example 3

Input
sheet = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Output
[0, 0]

Nothing is punched, so every row holds 0 and the smallest index wins the tie.

Constraints

  • 1 <= sheet.length <= 100
  • 1 <= sheet[i].length <= 100
  • 0 <= sheet[i][j] <= 1
  • Every row of sheet has the same number of columns.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def busiest_row(sheet: list[list[int]]) -> list[int]:
Java
public int[] busiestRow(int[][] sheet)