|
| 1 | +package LeetCodeJava.DFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/evaluate-division/description/ |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * 399. Evaluate Division |
| 9 | + * Solved |
| 10 | + * Medium |
| 11 | + * Topics |
| 12 | + * Companies |
| 13 | + * Hint |
| 14 | + * You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable. |
| 15 | + * |
| 16 | + * You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?. |
| 17 | + * |
| 18 | + * Return the answers to all queries. If a single answer cannot be determined, return -1.0. |
| 19 | + * |
| 20 | + * 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. |
| 21 | + * |
| 22 | + * Note: The variables that do not occur in the list of equations are undefined, so the answer cannot be determined for them. |
| 23 | + * |
| 24 | + * |
| 25 | + * |
| 26 | + * Example 1: |
| 27 | + * |
| 28 | + * Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]] |
| 29 | + * Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000] |
| 30 | + * Explanation: |
| 31 | + * Given: a / b = 2.0, b / c = 3.0 |
| 32 | + * queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? |
| 33 | + * return: [6.0, 0.5, -1.0, 1.0, -1.0 ] |
| 34 | + * note: x is undefined => -1.0 |
| 35 | + * Example 2: |
| 36 | + * |
| 37 | + * Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]] |
| 38 | + * Output: [3.75000,0.40000,5.00000,0.20000] |
| 39 | + * Example 3: |
| 40 | + * |
| 41 | + * Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]] |
| 42 | + * Output: [0.50000,2.00000,-1.00000,-1.00000] |
| 43 | + * |
| 44 | + * |
| 45 | + * Constraints: |
| 46 | + * |
| 47 | + * 1 <= equations.length <= 20 |
| 48 | + * equations[i].length == 2 |
| 49 | + * 1 <= Ai.length, Bi.length <= 5 |
| 50 | + * values.length == equations.length |
| 51 | + * 0.0 < values[i] <= 20.0 |
| 52 | + * 1 <= queries.length <= 20 |
| 53 | + * queries[i].length == 2 |
| 54 | + * 1 <= Cj.length, Dj.length <= 5 |
| 55 | + * Ai, Bi, Cj, Dj consist of lower case English letters and digits. |
| 56 | + * |
| 57 | + */ |
| 58 | +public class EvaluateDivision { |
| 59 | + // V0 |
| 60 | + // TODO: implement |
| 61 | +// public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) { |
| 62 | +// |
| 63 | +// } |
| 64 | + |
| 65 | + |
| 66 | + // V1 |
| 67 | + |
| 68 | + // V2 |
| 69 | + // IDEA: DFS |
| 70 | + // https://leetcode.com/problems/evaluate-division/solutions/3543256/image-explanation-easiest-concise-comple-okpu/ |
| 71 | + public void dfs(String node, String dest, HashMap<String, HashMap<String, Double>> gr, HashSet<String> vis, |
| 72 | + double[] ans, double temp) { |
| 73 | + if (vis.contains(node)) |
| 74 | + return; |
| 75 | + |
| 76 | + vis.add(node); |
| 77 | + if (node.equals(dest)) { |
| 78 | + ans[0] = temp; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + for (Map.Entry<String, Double> entry : gr.get(node).entrySet()) { |
| 83 | + String ne = entry.getKey(); |
| 84 | + double val = entry.getValue(); |
| 85 | + dfs(ne, dest, gr, vis, ans, temp * val); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public HashMap<String, HashMap<String, Double>> buildGraph(List<List<String>> equations, double[] values) { |
| 90 | + HashMap<String, HashMap<String, Double>> gr = new HashMap<>(); |
| 91 | + |
| 92 | + for (int i = 0; i < equations.size(); i++) { |
| 93 | + String dividend = equations.get(i).get(0); |
| 94 | + String divisor = equations.get(i).get(1); |
| 95 | + double value = values[i]; |
| 96 | + |
| 97 | + gr.putIfAbsent(dividend, new HashMap<>()); |
| 98 | + gr.putIfAbsent(divisor, new HashMap<>()); |
| 99 | + |
| 100 | + gr.get(dividend).put(divisor, value); |
| 101 | + gr.get(divisor).put(dividend, 1.0 / value); |
| 102 | + } |
| 103 | + |
| 104 | + return gr; |
| 105 | + } |
| 106 | + |
| 107 | + public double[] calcEquation_2(List<List<String>> equations, double[] values, List<List<String>> queries) { |
| 108 | + HashMap<String, HashMap<String, Double>> gr = buildGraph(equations, values); |
| 109 | + double[] finalAns = new double[queries.size()]; |
| 110 | + |
| 111 | + for (int i = 0; i < queries.size(); i++) { |
| 112 | + String dividend = queries.get(i).get(0); |
| 113 | + String divisor = queries.get(i).get(1); |
| 114 | + |
| 115 | + if (!gr.containsKey(dividend) || !gr.containsKey(divisor)) { |
| 116 | + finalAns[i] = -1.0; |
| 117 | + } else { |
| 118 | + HashSet<String> vis = new HashSet<>(); |
| 119 | + double[] ans = { -1.0 }; |
| 120 | + double temp = 1.0; |
| 121 | + dfs(dividend, divisor, gr, vis, ans, temp); |
| 122 | + finalAns[i] = ans[0]; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return finalAns; |
| 127 | + } |
| 128 | + |
| 129 | + // V3 |
| 130 | + // IDEA: DFS |
| 131 | + // https://leetcode.com/problems/evaluate-division/solutions/1992891/java-dfs-solution-with-comments-evaluate-6gmn/ |
| 132 | + private Map<String, Map<String, Double>> makeGraph(List<List<String>> e, double[] values) { |
| 133 | + // build a graph |
| 134 | + // like a -> b = values[i] |
| 135 | + // and b -> a = 1.0 / values[i]; |
| 136 | + Map<String, Map<String, Double>> graph = new HashMap<>(); |
| 137 | + String u, v; |
| 138 | + |
| 139 | + for (int i = 0; i < e.size(); i++) { |
| 140 | + u = e.get(i).get(0); |
| 141 | + v = e.get(i).get(1); |
| 142 | + |
| 143 | + graph.putIfAbsent(u, new HashMap<>()); |
| 144 | + graph.get(u).put(v, values[i]); |
| 145 | + |
| 146 | + graph.putIfAbsent(v, new HashMap<>()); |
| 147 | + graph.get(v).put(u, 1 / values[i]); |
| 148 | + |
| 149 | + } |
| 150 | + return graph; |
| 151 | + } |
| 152 | + |
| 153 | + public double[] calcEquation_3(List<List<String>> equations, double[] values, List<List<String>> queries) { |
| 154 | + Map<String, Map<String, Double>> graph = makeGraph(equations, values); |
| 155 | + |
| 156 | + double[] ans = new double[queries.size()]; |
| 157 | + |
| 158 | + // check for every Querie |
| 159 | + // store it in ans array; |
| 160 | + for (int i = 0; i < queries.size(); i++) { |
| 161 | + ans[i] = dfs(queries.get(i).get(0), queries.get(i).get(1), new HashSet<>(), graph); |
| 162 | + } |
| 163 | + return ans; |
| 164 | + } |
| 165 | + |
| 166 | + public double dfs(String src, String dest, Set<String> visited, Map<String, Map<String, Double>> graph) { |
| 167 | + // check the terminated Case |
| 168 | + // if string is not present in graph return -1.0; |
| 169 | + // like [a, e] or [x, x] :) |
| 170 | + if (graph.containsKey(src) == false) |
| 171 | + return -1.0; |
| 172 | + |
| 173 | + // simply say check src and dest are equal :) then return dest |
| 174 | + // store it in weight varaible; |
| 175 | + // case like [a,a] also handle |
| 176 | + if (graph.get(src).containsKey(dest)) { |
| 177 | + return graph.get(src).get(dest); |
| 178 | + } |
| 179 | + |
| 180 | + visited.add(src); |
| 181 | + |
| 182 | + for (Map.Entry<String, Double> nbr : graph.get(src).entrySet()) { |
| 183 | + if (visited.contains(nbr.getKey()) == false) { |
| 184 | + double weight = dfs(nbr.getKey(), dest, visited, graph); |
| 185 | + |
| 186 | + // if weight is not -1.0(terminate case) |
| 187 | + // then mutliply it |
| 188 | + // like in querie a -> c => 2 * 3 = 6 |
| 189 | + if (weight != -1.0) { |
| 190 | + return nbr.getValue() * weight; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + return -1.0; |
| 195 | + } |
| 196 | + |
| 197 | + // V4 |
| 198 | + // IDEA: BFS |
| 199 | + // https://leetcode.com/problems/evaluate-division/solutions/3543150/pythonjavacsimple-solutioneasy-to-unders-7uwo/ |
| 200 | +// public double[] calcEquation_4(List<List<String>> equations, double[] values, List<List<String>> queries) { |
| 201 | +// Map<String, Map<String, Double>> graph = buildGraph_4(equations, values); |
| 202 | +// double[] results = new double[queries.size()]; |
| 203 | +// |
| 204 | +// for (int i = 0; i < queries.size(); i++) { |
| 205 | +// List<String> query = queries.get(i); |
| 206 | +// String dividend = query.get(0); |
| 207 | +// String divisor = query.get(1); |
| 208 | +// |
| 209 | +// if (!graph.containsKey(dividend) || !graph.containsKey(divisor)) { |
| 210 | +// results[i] = -1.0; |
| 211 | +// } else { |
| 212 | +// results[i] = bfs(dividend, divisor, graph); |
| 213 | +// } |
| 214 | +// } |
| 215 | +// |
| 216 | +// return results; |
| 217 | +// } |
| 218 | +// |
| 219 | +// private Map<String, Map<String, Double>> buildGraph_4(List<List<String>> equations, double[] values) { |
| 220 | +// Map<String, Map<String, Double>> graph = new HashMap<>(); |
| 221 | +// |
| 222 | +// for (int i = 0; i < equations.size(); i++) { |
| 223 | +// List<String> equation = equations.get(i); |
| 224 | +// String dividend = equation.get(0); |
| 225 | +// String divisor = equation.get(1); |
| 226 | +// double value = values[i]; |
| 227 | +// |
| 228 | +// graph.putIfAbsent(dividend, new HashMap<>()); |
| 229 | +// graph.putIfAbsent(divisor, new HashMap<>()); |
| 230 | +// graph.get(dividend).put(divisor, value); |
| 231 | +// graph.get(divisor).put(dividend, 1.0 / value); |
| 232 | +// } |
| 233 | +// |
| 234 | +// return graph; |
| 235 | +// } |
| 236 | +// |
| 237 | +// private double bfs(String start, String end, Map<String, Map<String, Double>> graph) { |
| 238 | +// Queue<Pair<String, Double>> queue = new LinkedList<>(); |
| 239 | +// Set<String> visited = new HashSet<>(); |
| 240 | +// queue.offer(new Pair<>(start, 1.0)); |
| 241 | +// |
| 242 | +// while (!queue.isEmpty()) { |
| 243 | +// Pair<String, Double> pair = queue.poll(); |
| 244 | +// String node = pair.getKey(); |
| 245 | +// double value = pair.getValue(); |
| 246 | +// |
| 247 | +// if (node.equals(end)) { |
| 248 | +// return value; |
| 249 | +// } |
| 250 | +// |
| 251 | +// visited.add(node); |
| 252 | +// |
| 253 | +// for (Map.Entry<String, Double> neighbor : graph.get(node).entrySet()) { |
| 254 | +// String neighborNode = neighbor.getKey(); |
| 255 | +// double neighborValue = neighbor.getValue(); |
| 256 | +// |
| 257 | +// if (!visited.contains(neighborNode)) { |
| 258 | +// queue.offer(new Pair<>(neighborNode, value * neighborValue)); |
| 259 | +// } |
| 260 | +// } |
| 261 | +// } |
| 262 | +// |
| 263 | +// return -1.0; |
| 264 | +// } |
| 265 | +} |
0 commit comments