Skip to content

Commit 0319afc

Browse files
authored
Update find-a-good-subset-of-the-matrix.cpp
1 parent d3616b8 commit 0319afc

File tree

1 file changed

+3
-32
lines changed

1 file changed

+3
-32
lines changed

C++/find-a-good-subset-of-the-matrix.cpp

+3-32
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,9 @@ class Solution {
1414
if (!mask) {
1515
return {i};
1616
}
17-
for (int mask2 = ((1 << size(grid[0])) - 1) ^ mask, submask = mask2; submask > 0; submask = (submask - 1) & mask2) {
18-
if (lookup.count(submask)) {
19-
return {lookup[submask], i};
20-
}
21-
}
22-
lookup[mask] = i;
23-
}
24-
return {};
25-
}
26-
};
27-
28-
// Time: O(m * 2^n)
29-
// Space: O(2^n)
30-
// bitmasks, constructive algorithms, greedy
31-
class Solution2 {
32-
public:
33-
vector<int> goodSubsetofBinaryMatrix(vector<vector<int>>& grid) {
34-
unordered_map<int, int> lookup;
35-
for (int i = 0; i < size(grid); ++i) {
36-
int mask = 0;
37-
for (int j = 0; j < size(grid[0]); ++j) {
38-
mask |= grid[i][j] << j;
39-
}
40-
if (!mask) {
41-
return {i};
42-
}
43-
for (int mask2 = 1; mask2 < 1 << size(grid[0]); ++mask2) {
44-
if (mask & mask2) {
45-
continue;
46-
}
47-
if (lookup.count(mask2)) {
48-
return {lookup[mask2], i};
17+
for (const auto& [mask2, j] : lookup) {
18+
if ((mask2 & mask) == 0) {
19+
return {j, i};
4920
}
5021
}
5122
lookup[mask] = i;

0 commit comments

Comments
 (0)