-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3528_UnitConversionI.cpp
More file actions
59 lines (51 loc) · 2.11 KB
/
3528_UnitConversionI.cpp
File metadata and controls
59 lines (51 loc) · 2.11 KB
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
// Problem 3528: Unit Conversion I
// https://leetcode.com/problems/unit-conversion-i/description/
#include <functional>
#include <list>
#include <vector>
using namespace std;
class Solution {
public:
static const int MOD = 1e9 + 7;
vector<int> baseUnitConversions(vector<vector<int>> &conversions) {
int n = conversions.size();
// 設定答案,預設沒資料都是 -1
vector<int> baseUnitConv(n + 1, -1);
// 第 0 筆資料為 1 分 "第 0 筆資料單位"
baseUnitConv[0] = 1;
// 整理反向 map
unordered_map<int, list<pair<int, int>>> reverseMap;
for (const auto &conversion : conversions) {
int source = conversion[0], target = conversion[1],
factor = conversion[2];
reverseMap[target].emplace_back(source, factor);
}
// 設定 dfs function
function<void(int, int, int)> processSourceConv =
[&](int source, int target, int factor) {
if (baseUnitConv[source] == -1) {
// 假如 source 還沒設定,使用 reverseMap 往前設定
for (const auto &prev : reverseMap[source]) {
// 前一個節點使用 processSourceConv 尋找並設定
processSourceConv(prev.first, source, prev.second);
// 直到 source 確定設定完畢
if (baseUnitConv[source] != -1) {
break;
}
}
}
// 目標單位設定成 來源乘以倍數
long long conv = baseUnitConv[source] % MOD;
conv = (conv * factor % MOD) % MOD;
baseUnitConv[target] = static_cast<int>(conv);
};
// 跑迴圈處理每個 conversion
for (const auto &conversion : conversions) {
int source = conversion[0], target = conversion[1],
factor = conversion[2];
processSourceConv(source, target, factor);
}
// 回傳答案
return baseUnitConv;
}
};