-
Notifications
You must be signed in to change notification settings - Fork 0
/
399.hpp
52 lines (42 loc) · 1.28 KB
/
399.hpp
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
#ifndef LEETCODE_399_HPP
#define LEETCODE_399_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
vector<double>
calcEquation(vector<pair<string, string>> equations, vector<double> &values, vector<pair<string, string>> queries) {
unordered_map<string, unordered_map<string, double >> quot;
for (int i = 0; i < values.size(); ++i) {
auto &p = equations[i];
quot[p.first][p.first] = 1;
quot[p.second][p.second] = 1;
quot[p.first][p.second] = values[i];
quot[p.second][p.first] = 1 / values[i];
}
for (auto &k : quot) {
for (auto &i : k.second) {
for (auto &j: k.second) {
if (i != j) {
quot[i.first][j.first] = quot[i.first][k.first] * quot[k.first][j.first];
}
}
}
}
vector<double> res;
for (auto &q : queries) {
res.push_back(quot[q.first].count(q.second) ? quot[q.first][q.second] : -1);
}
return res;
}
};
#endif //LEETCODE_399_HPP