-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3858-minimum-bitwise-or-from-grid.cpp
More file actions
37 lines (37 loc) · 1.19 KB
/
3858-minimum-bitwise-or-from-grid.cpp
File metadata and controls
37 lines (37 loc) · 1.19 KB
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
class Solution {
public:
int minimumOR(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size(), ans = 0;
vector<vector<bool>> alive(n, vector<bool>(m, true));
vector<int> cnt(n, m);
for (int b = 20; b >= 0; b--) {
int must_take = false;
for (int i = 0; i < n; i++) {
// if all alive elements have this bit, we must take it
int c = 0;
for (int j = 0; j < m; j++) {
if (!alive[i][j]) continue;
if ((grid[i][j] & (1 << b)) == 0) continue;
c++;
}
if (c == cnt[i]) {
must_take = true;
break;
}
}
if (must_take) {
ans |= (1 << b);
continue;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!alive[i][j]) continue;
if ((grid[i][j] & (1 << b)) == 0) continue;
alive[i][j] = false;
cnt[i]--;
}
}
}
return ans;
}
};