LeetCode 399: Evaluate Division Solution
Master LeetCode problem 399 (Evaluate Division), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
399. Evaluate Division
Problem Explanation
Explanation
To solve this problem, we can use a graph and perform a graph traversal to find the answer for each query. We can represent the variables and their values as nodes and edges in the graph, with the edge weights being the values of the equations. We then use Depth First Search (DFS) to find a path from the numerator to the denominator in each query and calculate the result.
- Create a graph where each variable is a node and the value of the equation between variables is the weight of the edge.
- For each equation, add two edges (A->B with weight value and B->A with weight 1/value) to the graph.
- For each query, find a path from the numerator to the denominator using DFS. Multiply the weights of the edges along the path to get the result.
- If a path cannot be found or the variable is undefined, return -1.0 for that query.
Time Complexity: O(E + Q), where E is the number of equations and Q is the number of queries. Space Complexity: O(E), where E is the number of equations.
Solution Code
class Solution {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, Map<String, Double>> graph = new HashMap<>();
buildGraph(equations, values, graph);
double[] results = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
results[i] = dfs(queries.get(i).get(0), queries.get(i).get(1), new HashSet<>(), graph);
}
return results;
}
private void buildGraph(List<List<String>> equations, double[] values, Map<String, Map<String, Double>> graph) {
for (int i = 0; i < equations.size(); i++) {
String nodeA = equations.get(i).get(0);
String nodeB = equations.get(i).get(1);
double value = values[i];
graph.putIfAbsent(nodeA, new HashMap<>());
graph.get(nodeA).put(nodeB, value);
graph.putIfAbsent(nodeB, new HashMap<>());
graph.get(nodeB).put(nodeA, 1 / value);
}
}
private double dfs(String start, String end, Set<String> visited, Map<String, Map<String, Double>> graph) {
if (!graph.containsKey(start) || !graph.containsKey(end)) {
return -1.0;
}
if (start.equals(end)) {
return 1.0;
}
visited.add(start);
for (Map.Entry<String, Double> neighbor : graph.get(start).entrySet()) {
if (!visited.contains(neighbor.getKey())) {
double result = dfs(neighbor.getKey(), end, visited, graph);
if (result != -1.0) {
return result * neighbor.getValue();
}
}
}
return -1.0;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 399 (Evaluate Division)?
This page provides optimized solutions for LeetCode problem 399 (Evaluate Division) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 399 (Evaluate Division)?
The time complexity for LeetCode 399 (Evaluate Division) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 399 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 399 in Java, C++, or Python.