|
20 | 20 |
|
21 | 21 | ### 0-1) Types |
22 | 22 |
|
| 23 | +- Equation calculation |
| 24 | + - LC 399 |
| 25 | + |
23 | 26 | ### 0-2) Pattern |
24 | 27 |
|
25 | 28 | ## 1) General form |
@@ -405,8 +408,87 @@ class Solution: |
405 | 408 | ``` |
406 | 409 |
|
407 | 410 | ### 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 | +} |
410 | 492 | ``` |
411 | 493 |
|
412 | 494 | ### 2-5) Friend Circles |
|
0 commit comments