2482. Maximum Rows Covered by Columns
My accepted Python solution to LeetCode problem 2482, Maximum Rows Covered by Columns, running in 4ms.
- Difficulty: Medium
- Python
- Runtime 4ms
- Memory 17.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 4ms, memory 17.4MB, accepted 2026-01-02.
class Solution:
def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int:
from itertools import combinations
m, n = len(matrix), len(matrix[0])
# Convert each row to a bitmask
row_masks = []
for row in matrix:
mask = 0
for j, val in enumerate(row):
if val == 1:
mask |= (1 << j)
row_masks.append(mask)
max_covered = 0
# Try all combinations of numSelect columns
for cols in combinations(range(n), numSelect):
# Create column mask
col_mask = 0
for c in cols:
col_mask |= (1 << c)
# Count covered rows
covered = 0
for row_mask in row_masks:
# Row is covered if all 1s in row are in selected columns
if (row_mask & col_mask) == row_mask:
covered += 1
max_covered = max(max_covered, covered)
return max_covered