You are given a 0-indexed m x n
binary matrix matrix
and an integer numSelect
, which denotes the number of distinct columns you must select from matrix
.
Let us consider s = {c1, c2, ...., cnumSelect}
as the set of columns selected by you. A row row
is covered by s
if:
- For each cell
matrix[row][col]
(0 <= col <= n - 1
) wherematrix[row][col] == 1
,col
is present ins
or, - No cell in
row
has a value of1
.
You need to choose numSelect
columns such that the number of rows that are covered is maximized.
Return the maximum number of rows that can be covered by a set of numSelect
columns.
Example 1:
Input: matrix = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], numSelect = 2 Output: 3 Explanation: One possible way to cover 3 rows is shown in the diagram above. We choose s = {0, 2}. - Row 0 is covered because it has no occurrences of 1. - Row 1 is covered because the columns with value 1, i.e. 0 and 2 are present in s. - Row 2 is not covered because matrix[2][1] == 1 but 1 is not present in s. - Row 3 is covered because matrix[2][2] == 1 and 2 is 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.
Example 2:
Input: matrix = [[1],[0]], numSelect = 1 Output: 2 Explanation: Selecting the only column will result in both rows being covered since the entire matrix is selected. Therefore, we return 2.
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 12
matrix[i][j]
is either0
or1
.1 <= numSelect <= n
class Solution:
def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int:
rows = []
for row in matrix:
mask = reduce(or_, (1 << j for j, x in enumerate(row) if x), 0)
rows.append(mask)
ans = 0
for mask in range(1 << len(matrix[0])):
if mask.bit_count() != numSelect:
continue
t = sum((x & mask) == x for x in rows)
ans = max(ans, t)
return ans
class Solution {
public int maximumRows(int[][] matrix, int numSelect) {
int m = matrix.length, n = matrix[0].length;
int[] rows = new int[m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 1) {
rows[i] |= 1 << j;
}
}
}
int ans = 0;
for (int mask = 1; mask < 1 << n; ++mask) {
if (Integer.bitCount(mask) != numSelect) {
continue;
}
int t = 0;
for (int x : rows) {
if ((x & mask) == x) {
++t;
}
}
ans = Math.max(ans, t);
}
return ans;
}
}
class Solution {
public:
int maximumRows(vector<vector<int>>& matrix, int numSelect) {
int m = matrix.size(), n = matrix[0].size();
int rows[m];
memset(rows, 0, sizeof(rows));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j]) {
rows[i] |= 1 << j;
}
}
}
int ans = 0;
for (int mask = 1; mask < 1 << n; ++mask) {
if (__builtin_popcount(mask) != numSelect) {
continue;
}
int t = 0;
for (int x : rows) {
t += (x & mask) == x;
}
ans = max(ans, t);
}
return ans;
}
};
func maximumRows(matrix [][]int, numSelect int) (ans int) {
m, n := len(matrix), len(matrix[0])
rows := make([]int, m)
for i, row := range matrix {
for j, x := range row {
if x == 1 {
rows[i] |= 1 << j
}
}
}
for mask := 1; mask < 1<<n; mask++ {
if bits.OnesCount(uint(mask)) != numSelect {
continue
}
t := 0
for _, x := range rows {
if (x & mask) == x {
t++
}
}
if ans < t {
ans = t
}
}
return
}