Skip to content

Commit 1c9fad3

Browse files
solves #399: Evaluate Division in java
1 parent 41f5581 commit 1c9fad3

File tree

3 files changed

+131
-31
lines changed

3 files changed

+131
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@
303303
| 387 | [First Unique Character in String](https://leetcode.com/problems/first-unique-character-in-a-string) | [![Java](assets/java.png)](src/FirstUniqueCharacter.java) [![Python](assets/python.png)](python/first_unique_character_in_string.py) | |
304304
| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference) | [![Java](assets/java.png)](src/FindTheDifference.java) [![Python](assets/python.png)](python/find_the_difference.py) | |
305305
| 392 | [Is Subsequence](https://leetcode.com/problems/is-subsequence) | [![Java](assets/java.png)](src/IsSubsequence.java) [![Python](assets/python.png)](python/is_subsequence.py) | |
306+
| 399 | [Evaluate Division](https://leetcode.com/problems/evaluate-division) | [![Java](assets/java.png)](src/EvaluateDivision.java) | |
306307
| 401 | [Binary Watch](https://leetcode.com/problems/binary-watch) | [![Java](assets/java.png)](src/BinaryWatch.java) [![Python](assets/python.png)](python/binary_watch.py) | |
307308
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves) | [![Java](assets/java.png)](src/SumOfLeftLeaves.java) [![Python](assets/python.png)](python/sum_of_left_leaves.py) | |
308309
| 405 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal) | [![Java](assets/java.png)](src/ConvertNumberToHexadecimal.java) [![Python](assets/python.png)](python/convert_a_number_to_hexadecimal.py) | |

src/EvaluateDivision.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// https://leetcode.com/problems/evaluate-division
2+
// N = |equations|, M = |queries|
3+
// T: O(N + M*N) = O(MN)
4+
// S: O(N + M)
5+
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public class EvaluateDivision {
13+
public static double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
14+
final Map<String, Map<String, Double>> graph = createGraph(equations, values);
15+
return computeQueries(graph, queries);
16+
}
17+
18+
private static Map<String, Map<String, Double>> createGraph(List<List<String>> equations, double[] values) {
19+
final Map<String, Map<String, Double>> result = new HashMap<>();
20+
for (int i = 0 ; i < equations.size() ; i++) {
21+
final List<String> equation = equations.get(i);
22+
final String dividend = equation.get(0), divisor = equation.get(1);
23+
result.putIfAbsent(dividend, new HashMap<>());
24+
result.putIfAbsent(divisor, new HashMap<>());
25+
result.get(dividend).put(divisor, values[i]);
26+
result.get(divisor).put(dividend, 1 / values[i]);
27+
}
28+
return result;
29+
}
30+
31+
private static double[] computeQueries(Map<String, Map<String, Double>> graph, List<List<String>> queries) {
32+
final double[] result = new double[queries.size()];
33+
for (int i = 0 ; i < queries.size() ; i++) {
34+
final List<String> query = queries.get(i);
35+
final String dividend = query.get(0), divisor = query.get(1);
36+
if (!graph.containsKey(dividend) || !graph.containsKey(divisor)) {
37+
result[i] = -1;
38+
} else if (dividend.equals(divisor)) {
39+
result[i] = 1;
40+
} else {
41+
result[i] = computeDfs(graph, dividend, divisor);
42+
}
43+
}
44+
return result;
45+
}
46+
47+
private static double computeDfs(Map<String, Map<String, Double>> graph, String dividend, String divisor) {
48+
return dfs(graph, dividend, divisor, 1, new HashSet<>());
49+
}
50+
51+
private static double dfs(Map<String, Map<String, Double>> graph, String current, String target, double product, Set<String> visited) {
52+
if (current.equals(target)) {
53+
return product;
54+
}
55+
if (visited.contains(current)) {
56+
return -1;
57+
}
58+
visited.add(current);
59+
for (Map.Entry<String, Double> edges : graph.get(current).entrySet()) {
60+
final double result = dfs(graph, edges.getKey(), target, product * edges.getValue(), visited);
61+
if (result != -1) {
62+
return result;
63+
}
64+
}
65+
return -1;
66+
}
67+
}

src/HelloWorld.java

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,76 @@
1+
import java.util.*;
2+
13
public class HelloWorld {
2-
public int countNodes(TreeNode root) {
3-
if (root == null) {
4-
return 0;
4+
public static class Node {
5+
public int val;
6+
public List<Node> neighbors;
7+
public Node() {
8+
val = 0;
9+
neighbors = new ArrayList<>();
510
}
6-
7-
final int depth = depthTree(root) - 1;
8-
if (depth == 0) {
9-
return 1;
11+
public Node(int _val) {
12+
val = _val;
13+
neighbors = new ArrayList<>();
1014
}
11-
12-
int left = 0, right = (int) Math.pow(2, depth) - 1, middle;
13-
while (left <= right) {
14-
middle = left + (right - left) / 2;
15-
if (exists(root, depth, middle)) left = middle + 1;
16-
else right = middle - 1;
15+
public Node(int _val, ArrayList<Node> _neighbors) {
16+
val = _val;
17+
neighbors = _neighbors;
1718
}
1819

19-
return (int) Math.pow(2, depth) - 1 + left;
20+
@Override
21+
public String toString() {
22+
return "Node{" +
23+
"val=" + val +
24+
'}';
25+
}
2026
}
2127

22-
private static boolean exists(TreeNode root, int depth, int index) {
23-
TreeNode current = root;
24-
int value = -1;
25-
for (int i = 0 ; i < depth ; i++) {
26-
final int middle = (int) Math.pow(2, depth - 1 - i);
27-
if (index > value + middle) {
28-
current = current.right;
29-
value += middle;
30-
} else {
31-
current = current.left;
28+
public static Node cloneGraph(Node root) {
29+
final Queue<Node> queue = new LinkedList<>();
30+
final Map<Node, Node> oldToNew = new HashMap<>();
31+
final Set<Node> processed = new HashSet<>();
32+
queue.add(root);
33+
34+
while (!queue.isEmpty()) {
35+
final Node node = queue.poll();
36+
if (processed.contains(node)) {
37+
continue;
38+
}
39+
processed.add(node);
40+
41+
final Node newNode = oldToNew.getOrDefault(node, new Node(node.val));
42+
oldToNew.putIfAbsent(node, newNode);
43+
44+
for (Node edge : node.neighbors) {
45+
final Node newEdge = oldToNew.getOrDefault(edge, new Node(edge.val));
46+
oldToNew.putIfAbsent(edge, newEdge);
47+
newNode.neighbors.add(newEdge);
48+
queue.add(edge);
3249
}
3350
}
34-
return current != null;
51+
52+
return oldToNew.get(root);
3553
}
3654

37-
private static int depthTree(TreeNode root) {
38-
if (root == null) {
39-
return 0;
40-
}
55+
public static void main(String[] args) {
56+
final Node node1 = new Node(1);
57+
final Node node2 = new Node(2);
58+
final Node node3 = new Node(3);
59+
final Node node4 = new Node(4);
60+
61+
node1.neighbors.add(node2);
62+
node1.neighbors.add(node4);
63+
64+
node2.neighbors.add(node1);
65+
node2.neighbors.add(node3);
66+
67+
node3.neighbors.add(node2);
68+
node3.neighbors.add(node4);
69+
70+
node4.neighbors.add(node1);
71+
node4.neighbors.add(node3);
4172

42-
return 1 + Math.max(depthTree(root.left), depthTree(root.right));
73+
final Node clone = cloneGraph(node1);
74+
System.out.println(clone);
4375
}
44-
}
76+
}

0 commit comments

Comments
 (0)