forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-conversion-i.cpp
More file actions
29 lines (27 loc) · 808 Bytes
/
Copy pathunit-conversion-i.cpp
File metadata and controls
29 lines (27 loc) · 808 Bytes
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
// Time: O(n)
// Space: O(n)
// bfs
class Solution {
public:
vector<int> baseUnitConversions(vector<vector<int>>& conversions) {
static const int MOD = 1e9 + 7;
vector<vector<pair<int, int>>> adj(size(conversions) + 1);
for (const auto& c : conversions) {
adj[c[0]].emplace_back(c[1], c[2]);
}
vector<int> result(size(adj));
result[0] = 1;
vector<int> q = {0};
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& [v, w] : adj[u]) {
result[v] = (static_cast<int64_t>(result[u]) * w) % MOD;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return result;
}
};