-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path399. Evaluate Division.ts
80 lines (66 loc) · 1.37 KB
/
399. Evaluate Division.ts
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
75
76
77
78
79
80
/**
* Runtime 65 ms Beats 46.76%
* Memory 43.2 MB Beats 72.69%
*/
function calcEquation(
equations: string[][],
values: number[],
queries: string[][]
): number[] {
const graph = {};
for (let i = 0; i < equations.length; i++) {
const [a, b] = equations[i];
const value = values[i];
if (!graph[a]) {
graph[a] = {};
}
if (!graph[b]) {
graph[b] = {};
}
graph[a][b] = value;
graph[b][a] = 1 / value;
}
const dfs = (a: string, b: string, visited: Set<string>) => {
if (!(a in graph) || !(b in graph)) {
return -1.0;
}
if (a === b) {
return 1.0;
}
visited.add(a);
const neighbors = graph[a];
for (const neighbor in neighbors) {
if (!visited.has(neighbor)) {
const result = dfs(neighbor, b, visited);
if (result !== -1.0) {
return neighbors[neighbor] * result;
}
}
}
return -1.0;
};
const results: number[] = [];
for (const query of queries) {
const [a, b] = query;
const visited = new Set<string>();
const result = dfs(a, b, visited);
results.push(result);
}
return results;
}
console.log(
calcEquation(
[
['a', 'b'],
['b', 'c'],
],
[2.0, 3.0],
[
['a', 'c'],
['b', 'a'],
['a', 'e'],
['a', 'a'],
['x', 'x'],
]
)
); // [6.0, 0.5, -1.0, 1.0, -1.0]