LeetCode 2397. 被列覆盖的最多行数
原题链接
中等
作者:
我是java同学
,
2023-09-25 21:56:08
,
所有人可见
,
阅读 70
class Solution {
public:
int n, m;
vector<vector<int>> mat;
unordered_set<int> hash;
int res = 0;
void dfs(int u, int col, int numSelect) {
if (col == numSelect) {
int cnt = 0;
for (int i = 0; i < n; i ++ ) {
bool flag = true;
for (int j = 0; j < m; j ++ ) {
if (mat[i][j] == 1 && hash.count(j) == 0) {
flag = false;
break;
}
}
if (flag) cnt ++ ;
}
res = max(res, cnt);
} else {
for (int i = u; i < m; i ++ ) {
hash.insert(i);
dfs(i + 1, col + 1, numSelect);
hash.erase(i);
}
}
}
int maximumRows(vector<vector<int>>& matrix, int numSelect) {
n = matrix.size(), m = matrix[0].size();
mat = matrix;
dfs(0, 0, numSelect);
return res;
}
};