399. Evaluate Division

  • 41.2%

https://leetcode.com/problems/evaluate-division/description/

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector<double>& values,
vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive.
This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.


方法一:

https://discuss.leetcode.com/topic/58577/0ms-c-union-find-solution-easy-to-understand

0ms C++ Union-Find Solution [EASY to UNDERSTAND]

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
class Solution {
// date: 2016-09-12 location: Santa Clara City Library
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
unordered_map<string, Node*> map;
vector<double> res;
for (int i = 0; i < equations.size(); i ++) {
string s1 = equations[i].first, s2 = equations[i].second;
if (map.count(s1) == 0 && map.count(s2) == 0) {
map[s1] = new Node();
map[s2] = new Node();
map[s1] -> value = values[i];
map[s2] -> value = 1;
map[s1] -> parent = map[s2];
} else if (map.count(s1) == 0) {
map[s1] = new Node();
map[s1] -> value = map[s2] -> value * values[i];
map[s1] -> parent = map[s2];
} else if (map.count(s2) == 0) {
map[s2] = new Node();
map[s2] -> value = map[s1] -> value / values[i];
map[s2] -> parent = map[s1];
} else {
unionNodes(map[s1], map[s2], values[i], map);
}
}

for (auto query : queries) {
if (map.count(query.first) == 0 || map.count(query.second) == 0 || findParent(map[query.first]) != findParent(map[query.second]))
res.push_back(-1);
else
res.push_back(map[query.first] -> value / map[query.second] -> value);
}
return res;
}

private:
struct Node {
Node* parent;
double value = 0.0;
Node() {parent = this;}
};

void unionNodes(Node* node1, Node* node2, double num, unordered_map<string, Node*>& map) {
Node* parent1 = findParent(node1), *parent2 = findParent(node2);
double ratio = node2 -> value * num / node1 -> value;
for (auto it = map.begin(); it != map.end(); it ++)
if (findParent(it -> second) == parent1)
it -> second -> value *= ratio;
parent1 -> parent = parent2;
}

Node* findParent(Node* node) {
if (node -> parent == node)
return node;
node -> parent = findParent(node -> parent);
return node -> parent;
}
};

Update:

Please also check Java solutions below.

Special thanks to @iambright and @Scarlett_comeup.

方法二:

https://discuss.leetcode.com/topic/58312/c-0ms-hash-dfs-solution

c++ 0ms Hash+DFS solution

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
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>> m;
vector<double> res;
for (int i = 0; i < values.size(); ++i)
{
m[equations[i].first].insert(make_pair(equations[i].second,values[i]));
if(values[i]!=0)
m[equations[i].second].insert(make_pair(equations[i].first,1/values[i]));
}

for (auto i : query)
{
unordered_set<string> s;
double tmp = check(i.first,i.second,m,s);
if(tmp) res.push_back(tmp);
else res.push_back(-1);
}
return res;
}

double check(string up, string down,
unordered_map<string,unordered_map<string, double>> &m,
unordered_set<string> &s)
{
if(m[up].find(down) != m[up].end()) return m[up][down];
for (auto i : m[up])
{
if(s.find(i.first) == s.end())
{
s.insert(i.first);
double tmp = check(i.first,down,m,s);
if(tmp) return i.second*tmp;
}
}
return 0;
}
};
谢谢你,可爱的朋友。