-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
59 lines (50 loc) · 1.65 KB
/
main.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
class Solution {
public:
bool isPrintable(vector<vector<int>>& targetGrid) {
int m = targetGrid.size();
int n = targetGrid.front().size();
unordered_set<int> colors;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
colors.insert(targetGrid[i][j]);
}
}
auto unprint = [&](int c) -> bool {
bool found = false;
int u = numeric_limits<int>::max();
int d = numeric_limits<int>::min();
int l = numeric_limits<int>::max();
int r = numeric_limits<int>::min();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (targetGrid[i][j] != c) continue;
found = true;
u = min(u, i);
d = max(d, i);
l = min(l, j);
r = max(r, j);
}
}
if (!found) return false;
for (int i = u; i <= d; ++i)
for (int j = l; j <= r; ++j)
if (targetGrid[i][j] != c && targetGrid[i][j] != 0)
return false;
for (int i = u; i <= d; ++i)
for (int j = l; j <= r; ++j) targetGrid[i][j] = 0;
colors.erase(c);
return true;
};
while (!colors.empty()) {
bool found = false;
for (int c : colors) {
if (unprint(c)) {
found = true;
break;
};
}
if (!found) return false;
}
return true;
}
};