Maximum Rows Covered by Columns - Problem
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 1's 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 us consider selected = {c1, c2, ...., cnumSelect} as the set of columns selected by you. A row i is covered by selected if:
- For each cell where
matrix[i][j] == 1, the columnjis inselected. - Or, no cell in row
ihas a value of 1.
Return the maximum number of rows that can be covered by a set of numSelect columns.
Input & Output
Example 1 — Basic Coverage
$
Input:
matrix = [[0,0,0],[1,0,1]], numSelect = 2
›
Output:
2
💡 Note:
Select columns [0,2]: Row 0 has no 1s (covered), Row 1 has 1s at positions 0,2 which are both selected (covered). Total: 2 rows.
Example 2 — Partial Coverage
$
Input:
matrix = [[1],[0]], numSelect = 1
›
Output:
2
💡 Note:
Select column [0]: Row 0 has 1 at position 0 (covered), Row 1 has no 1s (covered). Both rows covered.
Example 3 — Impossible Full Coverage
$
Input:
matrix = [[1,0],[0,1]], numSelect = 1
›
Output:
1
💡 Note:
Can only select 1 column. Either column covers exactly 1 row, so maximum is 1.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 ≤ m, n ≤ 12
- matrix[i][j] is either 0 or 1
- 1 ≤ numSelect ≤ n
Visualization
Tap to expand
Understanding the Visualization
1
Input Matrix
Binary matrix with rows containing 0s and 1s
2
Column Selection
Choose exactly numSelect columns to maximize coverage
3
Coverage Count
Count rows where all 1s are in selected columns
Key Takeaway
🎯 Key Insight: Convert the problem to finding optimal column combinations using bit manipulation for efficient row coverage checking
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code