You are given an m x n binary matrix matrix and an integer numSelect.
Your goal is to select exactly numSelectdistinct 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 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.

Input: matrix =[[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect =2Output: 3Explanation:
One possible way to cover 3 rows is shown in the diagram above.We choose s ={0,2}.\- Row 0is covered because it has no occurrences of 1.\- Row 1is covered because the columns with value 1, i.e.0 and 2 are present
in s.\- Row 2is not covered because matrix[2][1]==1 but 1is not present in s.\- Row 3is covered because matrix[2][2]==1 and 2is present in s.Thus, we can cover three rows.Note that s ={1,2} will also cover 3 rows, but it can be shown that no more
than three rows can be covered.

Input: matrix =[[1],[0]], numSelect =1Output: 2Explanation:
Selecting the only column will result in both rows being covered since the
entire matrix is selected.
Since the number of columns is small (≤12), we can try all possible combinations of columns to select. For each combination, we check how many rows are covered. Using bitmasks makes it efficient to represent both the selected columns and the required columns for each row.
classSolution {
public:int maximumRows(vector<vector<int>>& matrix, int numSelect) {
int m = matrix.size(), n = matrix[0].size(), ans =0;
vector<int> rowMask(m, 0);
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (matrix[i][j]) rowMask[i] |= (1<< j);
for (int mask =0; mask < (1<< n); ++mask) {
if (__builtin_popcount(mask) != numSelect) continue;
int cnt =0;
for (int r : rowMask)
if ((r & mask) == r) cnt++;
ans = max(ans, cnt);
}
return ans;
}
};
funcmaximumRows(matrix [][]int, numSelectint) int {
m, n:= len(matrix), len(matrix[0])
rowMask:= make([]int, m)
fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifmatrix[i][j] ==1 {
rowMask[i] |=1<<j }
}
}
ans:=0formask:=0; mask < (1<<n); mask++ {
ifbits.OnesCount(uint(mask)) !=numSelect {
continue }
cnt:=0for_, r:=rangerowMask {
ifr&mask==r {
cnt++ }
}
ifcnt > ans {
ans = cnt }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintmaximumRows(int[][] matrix, int numSelect) {
int m = matrix.length, n = matrix[0].length, ans = 0;
int[] rowMask =newint[m];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (matrix[i][j]== 1) rowMask[i]|= (1 << j);
for (int mask = 0; mask < (1 << n); ++mask) {
if (Integer.bitCount(mask) != numSelect) continue;
int cnt = 0;
for (int r : rowMask)
if ((r & mask) == r) cnt++;
ans = Math.max(ans, cnt);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmaximumRows(matrix: Array<IntArray>, numSelect: Int): Int {
val m = matrix.size
val n = matrix[0].size
val rowMask = IntArray(m)
for (i in0 until m)
for (j in0 until n)
if (matrix[i][j] ==1) rowMask[i] = rowMask[i] or (1 shl j)
var ans = 0for (mask in0 until (1 shl n)) {
if (mask.countOneBits() != numSelect) continuevar cnt = 0for (r in rowMask)
if ((r and mask) == r) cnt++ ans = maxOf(ans, cnt)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defmaximumRows(self, matrix: list[list[int]], numSelect: int) -> int:
m, n = len(matrix), len(matrix[0])
row_mask = [sum(val << j for j, val in enumerate(row)) for row in matrix]
ans =0for mask in range(1<< n):
if bin(mask).count('1') != numSelect:
continue cnt = sum((r & mask) == r for r in row_mask)
ans = max(ans, cnt)
return ans