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 the matrix such that you cover as many rows as possible.

A row is considered covered if:

  • All the 1's in that row are in columns that you have selected, OR
  • The row contains no 1's at all (empty rows are always covered)

For example, if a row is [0, 1, 0, 1, 0] and you select columns {1, 3}, this row is covered because all its 1's are in your selected columns.

Return the maximum number of rows that can be covered by selecting exactly numSelect columns.

Input & Output

example_1.py โ€” Basic Case
$ Input: matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2
โ€บ Output: 3
๐Ÿ’ก Note: Selecting columns 1 and 2 covers 3 rows: Row 0 (no 1's), Row 2 (1's in cols 1,2), and Row 3 (1 in col 2). Row 1 has 1's in cols 0,2 but we didn't select col 0, so it's not covered.
example_2.py โ€” All Rows Covered
$ Input: matrix = [[1],[0],[0]], numSelect = 1
โ€บ Output: 3
๐Ÿ’ก Note: Selecting column 0 covers all 3 rows: Row 0 (1 in selected col 0), Row 1 (no 1's), Row 2 (no 1's). All rows without 1's are automatically covered.
example_3.py โ€” Optimal Selection
$ Input: matrix = [[1,0,0,0,0,1,1,0],[1,0,0,0,0,0,1,1],[1,0,0,0,0,0,1,1]], numSelect = 4
โ€บ Output: 3
๐Ÿ’ก Note: We can select columns {0,5,6,7} to cover all 3 rows. Row 0 needs cols 0,5,6. Rows 1,2 need cols 0,6,7. Selecting all four covers everything.

Visualization

Tap to expand
๐Ÿข Security Camera Optimization ProblemBuilding Floors (Rows):Floor 1Floor 2Floor 3Floor 4Camera Positions:๐Ÿ“นPos A๐Ÿ“นPos B๐Ÿ“นPos CBudget: Select 2 camerasCoverage Analysis:โœ… Floor 1: No critical areas - Always secureโŒ Floor 2: Critical areas in A,C - Need both camerasโŒ Floor 3: Critical area in B - Camera not selectedโœ… Floor 4: Critical area in C - Camera selected๐Ÿ’ก Key Strategy:1. Try all possible combinations of numSelect camera positions2. For each combination, count how many floors are fully securedResult: Cameras at positions A,C secure 2 out of 4 floors
Understanding the Visualization
1
Analyze Floor Plans
Each row represents a floor layout with important areas marked as 1's
2
Budget Constraint
You must install exactly numSelect cameras in different positions (columns)
3
Coverage Rule
A floor is secure only if ALL its important areas are monitored by your cameras
4
Optimization Goal
Find camera placement that secures the maximum number of floors
Key Takeaway
๐ŸŽฏ Key Insight: This is a combinatorial optimization problem where we must exhaustively try different combinations to find the optimal solution, but bit manipulation can significantly speed up the coverage checking process.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(C(n, numSelect) * m * n)

C(n, numSelect) combinations, each taking O(m * n) to evaluate all rows and columns

n
2n
โœ“ Linear Growth
Space Complexity
O(numSelect)

Space to store current combination of selected columns

n
2n
โšก Linearithmic Space

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 โ‰ค m, n โ‰ค 12
  • matrix[i][j] is either 0 or 1
  • 1 โ‰ค numSelect โ‰ค n
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen