forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bricks-falling-when-hit.cpp
95 lines (86 loc) · 3.08 KB
/
bricks-falling-when-hit.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
// Time: O(r * c)
// Space: O(r * c)
class Solution {
public:
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
static const vector<pair<int, int>> directions{{-1, 0}, { 1, 0},
{ 0, 1}, { 0, -1}};
const int R = grid.size();
const int C = grid[0].size();
const auto index = [&C](int r, int c) { return r * C + c; };
vector<vector<int>> hit_grid(grid);
for (const auto& hit : hits) {
hit_grid[hit[0]][hit[1]] = 0;
}
UnionFind union_find(R * C);
for (int r = 0; r < hit_grid.size(); ++r) {
for (int c = 0; c < hit_grid[r].size(); ++c) {
if (!hit_grid[r][c]) {
continue;
}
if (r == 0) {
union_find.union_set(index(r, c), R * C);
}
if (r && hit_grid[r - 1][c]) {
union_find.union_set(index(r, c), index(r - 1, c));
}
if (c && hit_grid[r][c - 1]) {
union_find.union_set(index(r, c), index(r, c - 1));
}
}
}
vector<int> result;
for (int i = hits.size() - 1; i >= 0; --i) {
const auto r = hits[i][0], c = hits[i][1];
const auto prev_roof = union_find.top();
if (grid[r][c] == 0) {
result.emplace_back(0);
continue;
}
for (const auto& d : directions) {
const auto nr = r + d.first, nc = c + d.second;
if (0 <= nr && nr < R &&
0 <= nc && nc < C &&
hit_grid[nr][nc]) {
union_find.union_set(index(r, c), index(nr, nc));
}
}
if (r == 0) {
union_find.union_set(index(r, c), R * C);
}
hit_grid[r][c] = 1;
result.emplace_back(max(0, union_find.top() - prev_roof - 1));
}
reverse(result.begin(), result.end());
return result;
}
private:
class UnionFind {
public:
UnionFind(const int n) : set_(n + 1), size_(n + 1, 1) {
iota(set_.begin(), set_.end(), 0);
size_.back() = 0;
}
int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
bool union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root == y_root) {
return false;
}
set_[min(x_root, y_root)] = max(x_root, y_root);
size_[max(x_root, y_root)] += size_[min(x_root, y_root)];
return true;
}
int top() {
return size_[find_set(size_.size() - 1)];
}
private:
vector<int> set_;
vector<int> size_;
};
};