forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluateDivision.java
More file actions
64 lines (49 loc) · 2.12 KB
/
EvaluateDivision.java
File metadata and controls
64 lines (49 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package graph;
import java.util.*;
/**
* Description: https://leetcode.com/problems/evaluate-division
* Difficulty: Medium
* Time complexity: O(n * queries)
* Space complexity: O(n)
*/
public class EvaluateDivision {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
Map<String, List<Edge>> adjList = buildAdjList(equations, values);
double[] results = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
List<String> query = queries.get(i);
results[i] = calculate(query.get(0), query.get(1), adjList);
}
return results;
}
private double calculate(String start, String target, Map<String, List<Edge>> adjList) {
if (!adjList.containsKey(start) || !adjList.containsKey(target)) return -1.0;
if (Objects.equals(start, target)) return 1.0;
Set<String> visited = new HashSet<>();
visited.add(start);
Queue<Edge> planned = new LinkedList<>();
planned.offer(new Edge(start, 1));
while (!planned.isEmpty()) {
Edge current = planned.poll();
for (Edge next : adjList.getOrDefault(current.node, List.of())) {
if (visited.add(next.node)) {
if (Objects.equals(next.node, target)) return next.weight * current.weight;
planned.offer(new Edge(next.node, next.weight * current.weight));
}
}
}
return -1.0;
}
private Map<String, List<Edge>> buildAdjList(List<List<String>> equations, double[] values) {
Map<String, List<Edge>> adjList = new HashMap<>();
for (int i = 0; i < equations.size(); i++) {
List<String> edge = equations.get(i);
double weight = values[i];
adjList.computeIfAbsent(edge.get(0), __ -> new ArrayList<>()).add(new Edge(edge.get(1), weight));
adjList.computeIfAbsent(edge.get(1), __ -> new ArrayList<>()).add(new Edge(edge.get(0), 1 / weight));
}
return adjList;
}
private record Edge(String node, double weight) {
}
}