|
| 1 | +""" |
| 2 | +author: Zhengjian Kang |
| 3 | +date: 03/13/2023 |
| 4 | +
|
| 5 | +https://leetcode.com/problems/evaluate-division/ |
| 6 | +399. Evaluate Division |
| 7 | +
|
| 8 | +note: 构建有向图, search for each query; each query has at most one potential solution |
| 9 | +You are given an array of variable pairs equations and an array of real numbers values, |
| 10 | +where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. |
| 11 | +Each Ai or Bi is a string that represents a single variable. |
| 12 | +
|
| 13 | +You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?. |
| 14 | +Return the answers to all queries. If a single answer cannot be determined, return -1.0. |
| 15 | +
|
| 16 | +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. |
| 17 | +
|
| 18 | +Example 1: |
| 19 | +Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]] |
| 20 | +Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000] |
| 21 | +Explanation: |
| 22 | +Given: a / b = 2.0, b / c = 3.0 |
| 23 | +queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? |
| 24 | +return: [6.0, 0.5, -1.0, 1.0, -1.0 ] |
| 25 | +
|
| 26 | +Example 2: |
| 27 | +Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]] |
| 28 | +Output: [3.75000,0.40000,5.00000,0.20000] |
| 29 | +
|
| 30 | +Example 3: |
| 31 | +Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]] |
| 32 | +Output: [0.50000,2.00000,-1.00000,-1.00000] |
| 33 | +
|
| 34 | +Constraints: |
| 35 | +1 <= equations.length <= 20 |
| 36 | +equations[i].length == 2 |
| 37 | +1 <= Ai.length, Bi.length <= 5 |
| 38 | +values.length == equations.length |
| 39 | +0.0 < values[i] <= 20.0 |
| 40 | +1 <= queries.length <= 20 |
| 41 | +queries[i].length == 2 |
| 42 | +1 <= Cj.length, Dj.length <= 5 |
| 43 | +Ai, Bi, Cj, Dj consist of lower case English letters and digits. |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +class Solution: |
| 48 | + def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: |
| 49 | + graph = defaultdict(list) |
| 50 | + for i in range(len(equations)): |
| 51 | + e1 = equations[i][0] |
| 52 | + e2 = equations[i][1] |
| 53 | + graph[e1].append((e2, values[i])) |
| 54 | + graph[e2].append((e1, 1.0 / values[i])) |
| 55 | + |
| 56 | + |
| 57 | + def dfs(src, dest, weight, visited): |
| 58 | + visited.add(src) |
| 59 | + if src == dest: |
| 60 | + return weight |
| 61 | + for neighbor, ratio in graph[src]: |
| 62 | + if neighbor in visited: |
| 63 | + continue |
| 64 | + val = dfs(neighbor, dest, weight * ratio, visited) |
| 65 | + if val is not None: return val |
| 66 | + return None |
| 67 | + |
| 68 | + res = [] |
| 69 | + for q1, q2 in queries: |
| 70 | + if q1 not in graph or q2 not in graph: |
| 71 | + res.append(-1.0) |
| 72 | + continue |
| 73 | + value = dfs(q1, q2, 1.0, set()) |
| 74 | + res.append(-1.0 if value is None else value) |
| 75 | + |
| 76 | + return res |
0 commit comments