Evaluate Division
MediumP3Coding
You are given equations of the form A / B = k: equations[i] = [A, B] and values[i] = k, where A and B are variable names and the values are positive reals. Then answer each query [C, D] with the value of C / D.
If the answer cannot be determined from the equations, return -1.0. If a variable in the query never appears in any equation, the answer is -1.0, even for x / x. The given equations are consistent with each other. Results are compared with a tolerance of 1e-5.
Example 1
- Input
- equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
- Output
- [6.0,0.5,-1.0,1.0,-1.0]
- Why
- a/c = a/b * b/c = 6.
Constraints
- 1 <= equations.length <= 20
- 1 <= variable name length <= 5
- 0.0 < values[i] <= 20.0
- 1 <= queries.length <= 20
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.