2397. Maximum Rows Covered by Columns
You are given a 0-indexed m x n binary matrix mat and an integer cols, which denotes the number of columns you must choose.
A row is covered by a set of columns if each cell in the row that has a value of 1 also lies in one of the columns of the chosen set.
Return the maximum number of rows that can be covered by a set of cols columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public: int maximumRows(vector<vector<int>>& mat, int cols) { int res = 0, n = mat.size(), m = mat[0].size(); for(int mask = 0; mask < (1<<m); mask++) { if(__builtin_popcount(mask) != cols) continue; int now = 0; for(int i = 0; i < n; i++) { bool pick = true; for(int j = 0; j < m; j++) { if(mask & (1<<j)) continue; if(mat[i][j]) pick = false; if(!pick) break; } if(pick) now++; } res = max(res,now); } return res; } };
|