Skip to content

Commit 5b53f12

Browse files
authored
Create maximum-good-people-based-on-statements.cpp
1 parent aceeef7 commit 5b53f12

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time: O(n^2 * 2^n)
2+
// Space: O(1)
3+
4+
// brute force, bitmask
5+
class Solution {
6+
public:
7+
int maximumGood(vector<vector<int>>& statements) {
8+
auto check = [&statements](int mask) {
9+
for (int i = 0; i < size(statements); ++i) {
10+
if (((mask >> i) & 1) == 0) {
11+
continue;
12+
}
13+
for (int j = 0; j < size(statements[i]); ++j) {
14+
if (statements[i][j] != 2 && ((mask >> j) & 1) != statements[i][j]) {
15+
return false;
16+
}
17+
}
18+
}
19+
return true;
20+
};
21+
22+
const int total = 1 << size(statements);
23+
int result = 0;
24+
for (int mask = 0; mask < total; ++mask) {
25+
if (check(mask)) {
26+
result = max(result, __builtin_popcount(mask));
27+
}
28+
}
29+
return result;
30+
}
31+
};

0 commit comments

Comments
 (0)