-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfind-subtree-sizes-after-changes.cpp
68 lines (64 loc) · 2.11 KB
/
find-subtree-sizes-after-changes.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
// Time: O(n)
// Space: O(n)
// iterative dfs, hash table
class Solution {
public:
vector<int> findSubtreeSizes(vector<int>& parent, string s) {
vector<vector<int>> adj(size(parent));
for (int v = 0; v < size(parent); ++v) {
if (parent[v] != -1) {
adj[parent[v]].emplace_back(v);
}
}
const auto& iter_dfs = [&]() {
vector<vector<int>> lookup(26);
vector<int> result(size(parent), 1);
vector<pair<int, int>> stk = {{1, 0}};
while (!empty(stk)) {
const auto [step, u] = stk.back(); stk.pop_back();
if (step == 1) {
lookup[s[u] - 'a'].emplace_back(u);
stk.emplace_back(2, u);
for (const auto& v : adj[u]) {
stk.emplace_back(1, v);
}
} else if (step == 2) {
for (const auto& v : adj[u]) {
const auto i = !empty(lookup[s[v] - 'a']) ? lookup[s[v] - 'a'].back() : u;
result[i] += result[v];
}
lookup[s[u] - 'a'].pop_back();
}
}
return result;
};
return iter_dfs();
}
};
// Time: O(n)
// Space: O(n)
// dfs, hash table
class Solution2 {
public:
vector<int> findSubtreeSizes(vector<int>& parent, string s) {
vector<vector<int>> adj(size(parent));
for (int v = 0; v < size(parent); ++v) {
if (parent[v] != -1) {
adj[parent[v]].emplace_back(v);
}
}
vector<vector<int>> lookup(26);
vector<int> result(size(parent), 1);
const function<void (int)> dfs = [&](int u) {
lookup[s[u] - 'a'].emplace_back(u);
for (const auto& v : adj[u]) {
dfs(v);
const auto i = !empty(lookup[s[v] - 'a']) ? lookup[s[v] - 'a'].back() : u;
result[i] += result[v];
}
lookup[s[u] - 'a'].pop_back();
};
dfs(0);
return result;
}
};