Title: #399 - Evaluate Division - LeetCode JavaScript Solutions
Open Graph Title: #399 - Evaluate Division - LeetCode JavaScript Solutions
X Title: #399 - Evaluate Division - LeetCode JavaScript Solutions
Description: You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represen...
Open Graph Description: You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represen...
X Description: You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represen...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/evaluate-division
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/evaluate-division","name":"#399 - Evaluate Division - LeetCode JavaScript Solutions","description":"You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represen...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {string[][]} equations\n * @param {number[]} values\n * @param {string[][]} queries\n * @return {number[]}\n */\nvar calcEquation = function(equations, values, queries) {\n const map = equations.reduce((result, [a, b], index) => {\n result.set(a, [...(result.get(a) ?? []), [b, values[index]]]);\n result.set(b, [...(result.get(b) ?? []), [a, 1 / values[index]]]);\n return result;\n }, new Map());\n\n function traverse([a, b], seen = new Set(), current = 1) {\n if (!map.has(a) || !map.has(b)) return -1;\n if (a === b) return current;\n seen.add(a);\n\n for (const [key, value] of map.get(a)) {\n if (seen.has(key)) continue;\n const result = traverse([key, b], seen, current * value);\n if (result) return result;\n }\n return null;\n }\n\n return queries.map(item => traverse(item) ?? -1);\n};","keywords":"LeetCode 399, #399 - Evaluate Division, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/evaluate-division |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/evaluate-division/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0399-evaluate-division.js |
| Array | https://leetcodejavascript.com/tags/array |
| String | https://leetcodejavascript.com/tags/string |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Breadth-First Search | https://leetcodejavascript.com/tags/breadth-first-search |
| Graph | https://leetcodejavascript.com/tags/graph |
| Union Find | https://leetcodejavascript.com/tags/union-find |
| Shortest Path | https://leetcodejavascript.com/tags/shortest-path |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow