-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathEvaluate Division.cpp
74 lines (51 loc) · 2.15 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Solution by Rahul Surana
***********************************************************
You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi]
and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.
You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must
find the answer for Cj / Dj = ?.
Return the answers to all queries. If a single answer cannot be determined, return -1.0.
Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero
and that there is no contradiction.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
map<string, vector<pair<string,double>>> adj;
map<string,bool> vis;
double ans;
bool dfs(string s, string e, double a){
if(s==e && adj.find(s) != adj.end()) { ans = a; return true; }
// if(vis[s]) { vis[s] = false; return false; }
vis[s] = true;
bool k = false;
for(auto z : adj[s]){
if(!vis[z.first]){
if(dfs(z.first,e,a*z.second)) { k = true; }
if(k) break;
}
}
vis[s] = false;
return k;
}
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
for(int i = 0 ; i < equations.size(); i++){
adj[equations[i][0]].push_back({equations[i][1],values[i]});
adj[equations[i][1]].push_back({equations[i][0],1/values[i]});
vis[equations[i][0]] = false;
vis[equations[i][1]] = false;
}
vector<double> ansQ;
for(int i = 0 ;i < queries.size(); i++){
if(dfs(queries[i][0],queries[i][1],1.0)){
ansQ.push_back(ans);
}
else{
ansQ.push_back(-1);
}
}
return ansQ;
}
};