Evaluate Division Medium / #399 / graph
You get equations of the form x / y = k between variables, and queries x / y. Answer each query from the equations, or with -1 if they say nothing about it (a variable no equation names, or two variables no chain of equations links). Our equations: p / r = 5, r / u = 0.5, r / s = 4; queries: p / s, s / p, p / q. The answers are [20, 0.05, -1].
input equations and values: equations equations[i][0] / equations[i][1] = values[i] between variables named by strings; queries: pairs of variables [c, d]. returns For each query the value of c / d that follows from the equations, or -1.0 if it does not follow: the two are not linked by equations, or a variable is in none of them ( x / x too). constraints 1 ≤ equations.length ≤ 20 values.length = equations.length 0 < values[i] ≤ 20 1 ≤ queries.length ≤ 20 equations[i], queries[j]: pairs of names of 1–5 lowercase letters and digits the equations do not contradict each other our 13 tests keep to them examples Open a test in the tests panel: it shows the input and the expected answer. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {string[][]} equations
* @param {number[]} values
* @param {string[][]} queries
* @return {number[]}
*/
function calcEquation(equations, values, queries) {
}
test submit
13 tests · limit 3 s
01 — equations = [["x", "y"], ["y", "z"]]
values = [4, 0.5]
queries = [["x", "z"], ["z", "x"], ["y", "y"], ["x", "w"], ["w", "w"]] copy 02 — equations = [["a", "b"], ["c", "d"]]
values = [2, 4]
queries = [["a", "d"], ["b", "a"], ["d", "c"]] copy 03 — equations = [["ab1", "cd"], ["cd", "e22"], ["e22", "x"]]
values = [1.5, 2, 4]
queries = [["ab1", "x"], ["x", "cd"], ["ab1", "ab1"]] copy 04 — equations = [["a", "b"], ["b", "c"], ["a", "c"]]
values = [2, 5, 10]
queries = [["c", "a"], ["b", "c"]] copy 05 — equations = [["m", "n"]]
values = [20]
queries = [["n", "m"], ["m", "m"], ["n", "n"], ["m", "k"]] copy 06 — equations = [["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"]]
values = [2, 2, 2, 2]
queries = [["a", "e"], ["e", "a"], ["c", "e"]] copy 07 — equations = [["k0", "2"], ["gy", "az"], ["2", "az"]]
values = [5, 0.08, 0.16]
queries = [["k0", "2"], ["k0", "az"], ["k0", "gy"], ["2", "k0"], ["2", "az"], ["2", "gy"], ["az", "k0"], ["az", "2"], ["az", "gy"], ["gy", "k0"], ["gy", "2"], ["gy", "az"]] copy 08 — equations = [["p3", "1nn"], ["9f", "wu3x"], ["om1xx", "wu3x"], ["1nn", "cqobs"], ["9f", "om1xx"]]
values = [1, 0.16, 0.04, 5, 4]
queries = [["wu3x", "1nn"], ["om1xx", "9f"], ["wu3x", "9f"], ["om1xx", "cqobs"], ["9f", "cqobs"], ["p3", "1nn"], ["wu3x", "om1xx"], ["wu3x", "om1xx"]] copy 09 — equations = [["tgkj", "cn2"], ["u3l", "cwv81"], ["h", "cwv81"], ["h", "tgkj"], ["h", "u3l"]]
values = [1.25, 0.2, 0.25, 0.25, 1.25]
queries = [["e", "e"], ["tgkj", "tgkj"], ["h", "w59"], ["6js", "cwv81"], ["cwv81", "tgkj"], ["cwv81", "tgkj"], ["u3l", "tgkj"], ["cwv81", "cn2"]] copy 10 — equations = [["cf", "5ym2"], ["5ym2", "cf"], ["ll7ah", "cf"], ["ll7ah", "wrt"], ["5ym2", "1vk"], ["1vk", "wrt"], ["cf", "wrt"], ["1vk", "cf"], ["5ym2", "ll7ah"], ["1vk", "5ym2"]]
values = [2, 0.5, 4, 16, 1, 2, 4, 0.5, 0.125, 1]
queries = [["5ym2", "cf"], ["5ym2", "ll7ah"], ["ll7ah", "1vk"], ["1vk", "ll7ah"], ["ll7ah", "1vk"], ["5ym2", "ll7ah"], ["wrt", "ll7ah"], ["5ym2", "1vk"]] copy 11 — equations = [["a2", "ropep"], ["ropep", "opgn"], ["b", "ropep"], ["ropep", "s"], ["ropep", "b4"], ["p12s", "ropep"], ["lzf6", "ropep"]]
values = [2, 8, 0.625, 0.5, 8, 0.125, 3.125]
queries = [["a2", "b"], ["a2", "opgn"], ["s", "opgn"], ["b", "b4"], ["lzf6", "b4"], ["b4", "a2"], ["opgn", "a2"], ["lzf6", "b"], ["b4", "opgn"], ["b", "p12s"]] copy 12 — equations = [["c4h4n", "b34hz"], ["mfi9h", "no4vq"], ["c4h4n", "no4vq"], ["no4vq", "e2skv"], ["zsy7g", "mfi9h"], ["b34hz", "mfi9h"], ["e2skv", "zsy7g"]]
values = [0.8, 0.25, 2, 0.2, 1, 10, 20]
queries = [["e2skv", "zsy7g"], ["zsy7g", "b34hz"], ["c4h4n", "e2skv"], ["no4vq", "mfi9h"], ["e2skv", "b34hz"], ["c4h4n", "mfi9h"]] copy 13 — equations = [["cqpyj", "d"], ["s", "4s"], ["e6", "ce"], ["s", "n2of"], ["6ijev", "c"], ["0", "qubl"], ["ce", "y8pmh"], ["0", "tw840"], ["w7bn", "b7"], ["qubl", "ir5"], ["9y4q", "6ijev"], ["ce", "n2of"], … 20 arrays]
values = [0.5, 5, 4, 20, 0.1, 1, 0.125, 0.1, 1, 0.5, 1, 0.5, … 20 numbers]
queries = [["p8uo", "qubl"], ["tw840", "9y4q"], ["9y4q", "qubl"], ["9iw5", "6t"], ["tw840", "p8uo"], ["9iw5", "e6"], ["cqpyj", "w7bn"], ["s", "y8pmh"], ["4s", "6t"], ["yj1", "cqpyj"], ["y8pmh", "s"], ["cqpyj", "c"], … 20 arrays] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.