Skip to content

Commit c460584

Browse files
committed
add 399 java
1 parent 9e67a2f commit c460584

File tree

5 files changed

+390
-9
lines changed

5 files changed

+390
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@
962962
339| [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) | [Python](./leetcode_python/Depth-First-Search/nested-list-weight-sum.py) | _O(n)_ | _O(h)_ | Easy |🔒,`good basic`,`bfs`, `linkedin`, `fb`| AGAIN*** (3)
963963
364| [Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/) | [Python](./leetcode_python/Depth-First-Search/nested-list-weight-sum-ii.py) | _O(n)_ | _O(h)_ | Medium |🔒, `linkedin`| AGAIN*
964964
366| [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) | [Python](./leetcode_python/Depth-First-Search/find-leaves-of-binary-tree.py) | _O(n)_ | _O(h)_ | Medium |🔒, `linkedin`| AGAIN*
965-
399| [Evaluate Division](https://leetcode.com/problems/evaluate-division/) | [Python](./leetcode_python/Depth-First-Search/evaluate-division.py) | _O(q * \|V\|!)_ | _O(e)_ | Medium |`union find`, `dfs`, `AGAIN`, `good trick` `google`, `fb`| AGAIN*********** (6)
965+
399| [Evaluate Division](https://leetcode.com/problems/evaluate-division/) | [Python](./leetcode_python/Depth-First-Search/evaluate-division.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/EvaluateDivision.java)| _O(q * \|V\|!)_ | _O(e)_ | Medium |`union find`, `dfs`, `AGAIN`, `good trick` `google`, `fb`| AGAIN*********** (7)
966966
417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [Python](./leetcode_python/Depth-First-Search/pacific-atlantic-water-flow.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/PacificAtlanticWaterFlow.java) | _O(m * n)_ | _O(m * n)_ | Medium |Curated Top 75, 2 direction dfs, dfs, matrix, `m$`,`google`| AGAIN** (3)
967967
464| [Can I Win](https://leetcode.com/problems/can-i-win/) | [Python](./leetcode_python/Depth-First-Search/can-i-win.py) | _O(n!)_ | _O(n)_ | Medium |`DP`, `linkedin`| AGAIN* (not start)
968968
491| [Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/) | [Python](./leetcode_python/Depth-First-Search/increasing_subsequences.py) | | | Medium |`trick`,`dfs`,`dfs good trick`, `dp`,`yahoo` ,`fb`| AGAIN***** (2)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20250105:
1+
20250105: 399
22
20250104: 1031,369
33
20250101: 729,731
44
20241231: 769,817,855(again)

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-03-01 -> ['399']
12
2025-02-28 -> ['1031,369']
23
2025-02-25 -> ['729,731']
34
2025-02-24 -> ['769,817,855(again)']
@@ -7,33 +8,34 @@
78
2025-02-15 -> ['369,311']
89
2025-02-14 -> ['370']
910
2025-02-13 -> ['815,871,593,1109']
11+
2025-02-08 -> ['399']
1012
2025-02-07 -> ['1031,369', '560,523']
1113
2025-02-04 -> ['729,731']
1214
2025-02-03 -> ['769,817,855(again)']
1315
2025-02-01 -> ['498(again),934', '304,853,325']
1416
2025-01-31 -> ['379,173']
1517
2025-01-30 -> ['079,212(todo),362,849']
16-
2025-01-26 -> ['370(todo)']
18+
2025-01-26 -> ['399', '370(todo)']
1719
2025-01-25 -> ['1031,369', '369,311']
1820
2025-01-24 -> ['370', '34,767']
1921
2025-01-23 -> ['815,871,593,1109']
2022
2025-01-22 -> ['729,731']
2123
2025-01-21 -> ['769,817,855(again)']
2224
2025-01-20 -> ['722,380']
2325
2025-01-19 -> ['498(again),934', '33,81']
24-
2025-01-18 -> ['379,173']
26+
2025-01-18 -> ['399', '379,173']
2527
2025-01-17 -> ['1031,369', '079,212(todo),362,849', '560,523', '253']
2628
2025-01-16 -> ['776,31']
2729
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2830
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
29-
2025-01-13 -> ['769,817,855(again)']
31+
2025-01-13 -> ['399', '769,817,855(again)']
3032
2025-01-12 -> ['1031,369', '369,311']
3133
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
32-
2025-01-10 -> ['379,173', '815,871,593,1109', '833,950']
34+
2025-01-10 -> ['399', '379,173', '815,871,593,1109', '833,950']
3335
2025-01-09 -> ['1031,369', '729,731', '079,212(todo),362,849']
34-
2025-01-08 -> ['769,817,855(again)']
35-
2025-01-07 -> ['1031,369']
36-
2025-01-06 -> ['1031,369', '729,731', '498(again),934']
36+
2025-01-08 -> ['399', '769,817,855(again)']
37+
2025-01-07 -> ['399', '1031,369']
38+
2025-01-06 -> ['399', '1031,369', '729,731', '498(again),934']
3739
2025-01-05 -> ['1031,369', '769,817,855(again)', '379,173', '370(todo)']
3840
2025-01-04 -> ['729,731', '079,212(todo),362,849', '369,311', '560,523', '53,210,207']
3941
2025-01-03 -> ['729,731', '769,817,855(again)', '498(again),934', '370', '34,767', '444']
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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

Comments
 (0)