forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate-division.cpp
48 lines (44 loc) · 1.59 KB
/
evaluate-division.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
// Time: O(e + q * |V|!), |V| is the number of variables
// Space: O(e)
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations,
vector<double>& values, vector<pair<string, string>> query) {
unordered_map<string, unordered_map<string, double>> lookup;
for (int i = 0; i < values.size(); ++i) {
lookup[equations[i].first].emplace(equations[i].second, values[i]);
if (values[i] != 0) {
lookup[equations[i].second].emplace(equations[i].first, 1 / values[i]);
}
}
vector<double> result;
for (const auto& i : query) {
unordered_set<string> visited;
const auto tmp = check(i.first, i.second, lookup, &visited);
if (tmp.first) {
result.emplace_back(tmp.second);
} else {
result.emplace_back(-1);
}
}
return result;
}
private:
pair<bool, double> check(string up, string down,
unordered_map<string, unordered_map<string, double>> &lookup,
unordered_set<string> *visited) {
if (lookup[up].find(down) != lookup[up].end()) {
return {true, lookup[up][down]};
}
for (const auto& q : lookup[up]) {
if (!visited->count(q.first)) {
visited->emplace(q.first);
const auto tmp = check(q.first, down, lookup, visited);
if (tmp.first) {
return {true, q.second * tmp.second};
}
}
}
return {false, 0};
}
};