https://leetcode.cn/problems/maximum-rows-covered-by-columns/description/?envType=daily-question&envId=2024-01-04
class Solution {
public:
void processMat(vector<vector<int>> &mat, int cur) {
for (int i = 0; i < mat.size(); i++) {
mat[i][cur] = 0;
}
}
int getMaxRows(vector<vector<int>>& mat) {
int res = 0;
for (auto row : mat) {
int flag = 0;
for(auto ele : row) {
flag |= ele;
if (flag) break;
}
if (flag == 0) res++;
}
return res;
}
int res = 0;
void dfs(vector<vector<int>> mat, int cur, int coveredRows, int numSelect) {
if (cur == mat[0].size() || numSelect <= 0 || mat[0].size() - cur < numSelect) {
res = max(res, coveredRows);
return;
};
vector<vector<int>> tmp = mat;
processMat(tmp, cur);
dfs(tmp, cur + 1, getMaxRows(tmp), numSelect - 1);
dfs(mat, cur + 1, coveredRows, numSelect);
}
int maximumRows(vector<vector<int>>& matrix, int numSelect) {
dfs(matrix, 0, 0, numSelect);
return res;
}
};