Skip to content

Commit e2054e2

Browse files
committed
update cheatsheet
1 parent aabd26a commit e2054e2

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

doc/cheatsheet/java_trick.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ for (int i = 0; i < equations.size(); i++) {
110110
}
111111
```
112112

113+
- NOTE: can use nested map structure (e.g. `HashMap<String, HashMap<String, Double>>`) to store `keyA - valB - resC` info
114+
113115
### 1-0) String to Char array
114116
```java
115117
// java

doc/cheatsheet/union_find.md

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
### 0-1) Types
2222

23+
- Equation calculation
24+
- LC 399
25+
2326
### 0-2) Pattern
2427

2528
## 1) General form
@@ -405,8 +408,87 @@ class Solution:
405408
```
406409

407410
### 2-4) Evaluate Division
408-
```python
409-
# LC 399 Evaluate Division
411+
412+
```java
413+
// java
414+
// LC 399
415+
416+
// V4
417+
// IDEA: UNION FIND (gpt)
418+
class UnionFind {
419+
private Map<String, String> parent;
420+
private Map<String, Double> ratio;
421+
422+
public UnionFind() {
423+
this.parent = new HashMap<>();
424+
this.ratio = new HashMap<>();
425+
}
426+
427+
// Finds the root of a node and applies path compression
428+
public String find(String x) {
429+
if (!parent.containsKey(x)) {
430+
parent.put(x, x);
431+
ratio.put(x, 1.0);
432+
}
433+
434+
if (!x.equals(parent.get(x))) {
435+
String originalParent = parent.get(x);
436+
parent.put(x, find(originalParent));
437+
ratio.put(x, ratio.get(x) * ratio.get(originalParent));
438+
}
439+
440+
return parent.get(x);
441+
}
442+
443+
// Union two nodes with the given value
444+
public void union(String x, String y, double value) {
445+
String rootX = find(x);
446+
String rootY = find(y);
447+
448+
if (!rootX.equals(rootY)) {
449+
parent.put(rootX, rootY);
450+
ratio.put(rootX, value * ratio.get(y) / ratio.get(x));
451+
}
452+
}
453+
454+
// Get the ratio between two nodes if they are connected
455+
public double isConnected(String x, String y) {
456+
if (!parent.containsKey(x) || !parent.containsKey(y)) {
457+
return -1.0;
458+
}
459+
460+
String rootX = find(x);
461+
String rootY = find(y);
462+
463+
if (!rootX.equals(rootY)) {
464+
return -1.0;
465+
}
466+
467+
return ratio.get(x) / ratio.get(y);
468+
}
469+
}
470+
471+
public double[] calcEquation_4(List<List<String>> equations, double[] values, List<List<String>> queries) {
472+
UnionFind uf = new UnionFind();
473+
474+
// Build the union-find structure
475+
for (int i = 0; i < equations.size(); i++) {
476+
String a = equations.get(i).get(0);
477+
String b = equations.get(i).get(1);
478+
double value = values[i];
479+
uf.union(a, b, value);
480+
}
481+
482+
// Process the queries
483+
double[] results = new double[queries.size()];
484+
for (int i = 0; i < queries.size(); i++) {
485+
String c = queries.get(i).get(0);
486+
String d = queries.get(i).get(1);
487+
results[i] = uf.isConnected(c, d);
488+
}
489+
490+
return results;
491+
}
410492
```
411493

412494
### 2-5) Friend Circles

0 commit comments

Comments
 (0)