-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0399-evaluate-division.js
49 lines (45 loc) · 1.68 KB
/
0399-evaluate-division.js
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
/**
* 399. Evaluate Division
* https://leetcode.com/problems/evaluate-division/
* Difficulty: Medium
*
* 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.
*
* Note: The variables that do not occur in the list of equations are undefined, so the
* answer cannot be determined for them.
*/
/**
* @param {string[][]} equations
* @param {number[]} values
* @param {string[][]} queries
* @return {number[]}
*/
var calcEquation = function(equations, values, queries) {
const map = equations.reduce((result, [a, b], index) => {
result.set(a, [...(result.get(a) ?? []), [b, values[index]]]);
result.set(b, [...(result.get(b) ?? []), [a, 1 / values[index]]]);
return result;
}, new Map());
function traverse([a, b], seen = new Set(), current = 1) {
if (!map.has(a) || !map.has(b)) return -1;
if (a === b) return current;
seen.add(a);
for (const [key, value] of map.get(a)) {
if (seen.has(key)) continue;
const result = traverse([key, b], seen, current * value);
if (result) return result;
}
return null;
}
return queries.map(item => traverse(item) ?? -1);
};