forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstrange-printer-ii.cpp
138 lines (131 loc) · 4.62 KB
/
strange-printer-ii.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Time: O(c * m * n + e), c is the number of colors
// , e is the number of edges in adj, at most O(c^2)
// Space: O(e)
class Solution {
public:
bool isPrintable(vector<vector<int>>& targetGrid) {
unordered_map<int, vector<int>> boxes;
for (int r = 0; r < size(targetGrid); ++r) {
for (int c = 0; c < size(targetGrid[r]); ++c) {
int color = targetGrid[r][c];
if (!boxes.count(color)) {
boxes[color] = {r, c, r, c};
} else {
boxes[color][0] = min(boxes[color][0], r);
boxes[color][1] = min(boxes[color][1], c);
boxes[color][2] = max(boxes[color][2], r);
boxes[color][3] = max(boxes[color][3], c);
}
}
}
unordered_map<int, unordered_set<int>> adj;
for (const auto& [color, box] : boxes) {
for (int r = box[0]; r <= box[2]; ++r) {
for (int c = box[1]; c <= box[3]; ++c) {
if (targetGrid[r][c] != color) {
adj[color].emplace(targetGrid[r][c]);
}
}
}
}
unordered_map<int, int> lookup;
for (const auto& [color, _] : boxes) {
if (lookup.count(color)) {
continue;
}
if (hasCycle(adj, color, &lookup)) {
return false;
}
}
return true;
}
private:
enum State {VISITING, VISITED};
bool hasCycle(const unordered_map<int, unordered_set<int>>& adj,
int color,
unordered_map<int, int> *lookup) {
vector<pair<int, int>> stk = {{1, color}};
while (!empty(stk)) {
const auto [step, color] = stk.back(); stk.pop_back();
if (step == 1) {
(*lookup)[color] = VISITING;
stk.emplace_back(2, color);
if (!adj.count(color)) {
continue;
}
for (const auto& new_color : adj.at(color)) {
if (lookup->count(new_color)) {
if ((*lookup)[new_color] == VISITED) {
continue;
}
return true; // VISITING
}
stk.emplace_back(1, new_color);
}
} else {
(*lookup)[color] = VISITED;
}
}
return false;
}
};
// Time: O(c * m * n + e), c is the number of colors
// , e is the number of edges in adj, at most O(c^2)
// Space: O(e)
class Solution2 {
public:
bool isPrintable(vector<vector<int>>& targetGrid) {
static const int MAX_COLOR = 60;
unordered_map<int, unordered_set<int>> adj;
for (int color = 1; color <= MAX_COLOR; ++color) {
int min_r = size(targetGrid);
int min_c = size(targetGrid[0]);
int max_r = -1;
int max_c = -1;
for (int r = 0; r < size(targetGrid); ++r) {
for (int c = 0; c < size(targetGrid[r]); ++c) {
if (targetGrid[r][c] == color) {
min_r = min(min_r, r);
min_c = min(min_c, c);
max_r = max(max_r, r);
max_c = max(max_c, c);
}
}
}
for (int r = min_r; r <= max_r; ++r) {
for (int c = min_c; c <= max_c; ++c) {
if (targetGrid[r][c] != color) {
adj[color].emplace(targetGrid[r][c]);
}
}
}
}
unordered_map<int, int> lookup;
for (int color = 1; color <= MAX_COLOR; ++color) {
if (lookup.count(color)) {
continue;
}
if (hasCycle(adj, color, &lookup)) {
return false;
}
}
return true;
}
private:
enum State {VISITING, VISITED};
bool hasCycle(const unordered_map<int, unordered_set<int>>& adj,
int color,
unordered_map<int, int> *lookup) {
(*lookup)[color] = VISITING;
if (adj.count(color)) {
for (const auto& new_color : adj.at(color)) {
if ((!lookup->count(new_color) && hasCycle(adj, new_color, lookup)) ||
(*lookup)[new_color] == VISITING) {
return true;
}
}
}
(*lookup)[color] = VISITED;
return false;
}
};