-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2397.被列覆盖的最多行数.cpp
64 lines (61 loc) · 1.54 KB
/
2397.被列覆盖的最多行数.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @lc app=leetcode.cn id=2397 lang=cpp
*
* [2397] 被列覆盖的最多行数
*/
#include "a_header.h"
// @lc code=start
class Solution
{
private:
int maxCovered;
public:
int numOfCovered(vector<vector<int>> &matrix,
unordered_set<int> &selected)
{
int ret = matrix.size();
for (size_t i = 0; i < matrix.size(); i++)
{
int flag = 0;
for (size_t j = 0; j < matrix[0].size(); j++)
{
if (selected.find(j) == selected.end())
{
flag |= matrix[i][j];
if (flag)
{
ret--;
break;
}
}
}
}
return ret;
}
void backtrack(vector<vector<int>> &matrix,
unordered_set<int> &selected,
int col,
int numSelect)
{
if (selected.size() == numSelect)
{
maxCovered = max(maxCovered, numOfCovered(matrix, selected));
return;
}
for (int i = col; i < matrix[0].size(); i++)
{
selected.insert(i);
backtrack(matrix, selected, i + 1, numSelect);
selected.erase(i);
}
}
int maximumRows(vector<vector<int>> &matrix,
int numSelect)
{
maxCovered = -1;
unordered_set<int> selected;
backtrack(matrix, selected, 0, numSelect);
return maxCovered;
}
};
// @lc code=end