Maximum Rows Covered by Columns - Enumeration w/ Bitmasks [JS]
Solution: Enumeration w/ Bitmasks
Enumerate every combination of selected columns using bitmasks (1
to 2^n
).
For each combination, count the number of rows where every matrix[row][col] = 1
is covered by the current selected columns.
Small optimization: We don't need to consider cells with value of 0
, so for each row, we can store the columns where matrix[row][col] = 1
. This way, we don't have to unnecessarily go through every cell.
m = number of rows
, n = number of columns
Time Complexity: O(2^n * mn)
135ms
Space Complexity: O(mn)
44.8MB
var maximumRows = function(matrix, numSelect) {
let m = matrix.length, n = matrix[0].length, ones = Array(m).fill(0).map(() => []);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (matrix[i][j] === 1) {
ones[i].push(j); // store columns where matrix[row][col] = 1
}
}
}
let ans = 0;
for (let mask = 1; mask < (1 << n); mask++) {
if (countOnes(mask) !== numSelect) continue; // we need to select exactly numSelect columns
let rowsCovered = 0;
for (let row = 0; row < m; row++) {
let isCovered = true;
for (let one of ones[row]) {
let columnIsPresent = (mask >> one) & 1;
if (!columnIsPresent) { // found a matrix[row][col] = 1 where col is not present in s
isCovered = false;
break;
}
}
rowsCovered += isCovered ? 1 : 0;
}
ans = Math.max(ans, rowsCovered);
}
return ans;
};
function countOnes(num) {
let ones = 0;
while (num > 0) {
ones += (num & 1);
num >>= 1;
}
return ones;
}
Comments
Post a Comment