Maximum Rows Covered by Columns

You are given an m x n binary matrix matrix and an integer numSelect.

Your goal is to select exactly numSelect distinct columns from matrix such that you cover as many rows as possible.

A row is considered covered if all the 1s in that row are also part of a column that you have selected. If a row does not have any 1s, it is also considered covered.

More formally, let selected = {c1, c2, ..., cnumSelect} be the set of columns selected by you. A row i is covered by selected if:

  • For each cell where matrix[i][j] == 1, the column j is in selected.
  • Or, no cell in row i has a value of 1.

Return the maximum number of rows that can be covered by a set of numSelect columns.

Example 1
0 0 0
1 0 1
0 1 1
0 0 1
Inputmatrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2
Output3
Choosing columns 0 and 2 covers rows 0, 1, and 3, and no selection can cover more than three rows.
Example 2
1
0
Inputmatrix = [[1],[0]], numSelect = 1
Output2
Selecting the only column covers the entire matrix, so both rows are covered.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 12
  • matrix[i][j] is either 0 or 1.
  • 1 <= numSelect <= n

Asked at 1 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