-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluateDivision.cpp
More file actions
45 lines (42 loc) · 1.44 KB
/
evaluateDivision.cpp
File metadata and controls
45 lines (42 loc) · 1.44 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
// Source: https://leetcode.com/problems/evaluate-division/
// Author: Miao Zhang
// Date: 2021-02-07
class Solution {
public:
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
//g[A][B] = k -> A/B = k
unordered_map<string, unordered_map<string, double>> graph;
for (int i = 0; i < equations.size(); i++) {
string a = equations[i][0];
string b = equations[i][1];
double v = values[i];
graph[a][b] = v;
graph[b][a] = 1.0 / v;
}
vector<double> res;
for (auto q: queries) {
string x = q[0];
string y = q[1];
if (!graph.count(x) || !graph.count(y)) {
res.push_back(-1.0);
continue;
}
unordered_set<string> visited;
res.push_back(divide(x, y, graph, visited));
}
return res;
}
private:
double divide(string &a, string &b,
unordered_map<string, unordered_map<string, double>> &graph,
unordered_set<string> &visited) {
if (a == b) return 1.0;
visited.insert(a);
for (auto p: graph[a]) {
string c = p.first;
if (visited.count(c)) continue;
double d = divide(c, b, graph, visited);
if (d > 0) return d * graph[a][c];
}
return -1.0;
}