Practice/Amazon/Leetcode 399. Evaluate Division
CodingOptional
You are given a set of equations that define division relationships between variables, along with their numeric results. Each equation is represented as a pair of variables, and each result is a positive real number.
For example, if you're given the equation ["x", "y"] with value 2.5, this means that x / y = 2.5.
Your task is to answer a series of queries, where each query asks for the result of dividing one variable by another. If a query can be answered using the given equations (either directly or through a chain of relationships), return the numeric result. If a query cannot be answered (because one or both variables are undefined, or there's no connection between them), return -1.0.
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] Explanation:
Example 2:
Input: equations = [["a","b"],["c","d"]] values = [2.0, 4.0] queries = [["a","d"]] Output: [-1.0] Explanation: Variables a,b form one connected component and c,d form another. No path exists between 'a' and 'd', so return -1.0.