Skip to content

Commit de1c9a5

Browse files
author
yennj12
committed
add
1 parent 8e29716 commit de1c9a5

File tree

3 files changed

+255
-56
lines changed

3 files changed

+255
-56
lines changed

doc/cheatsheet/tree.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class Solution:
150150

151151
while True:
152152
# NOTE !!! : we GO THROUGH left sub tree to the end first, and form our stack on the same time, then do in-order transversal
153+
"""
154+
Why the Left Traversal is Necessary ?
155+
156+
To process nodes in in-order (left -> root -> right), you need to traverse the left subtree of every node before processing the node itself. This is a characteristic of in-order traversal—without traversing left first, you can't get the correct order.
157+
158+
In summary, the `hile root: stack.append(root); root = root.left part is critical for simulating the recursive left-first behavior in an iterative manner. There’s no way to completely skip this step if you want an in-order traversal (using an iterative approach), but alternatives like Morris traversal exist to avoid using a stack and still traverse efficiently.
159+
160+
"""
153161
while root:
154162
stack.append(root)
155163
root = root.left
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package dev;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* UnionFind
10+
*
11+
* given input,
12+
*
13+
* 1) check if 2 nodes are connected
14+
* 2) find one nodes' parent
15+
*
16+
*/
17+
18+
public class UnionFindTest1 {
19+
20+
// attr
21+
Integer[] parents;
22+
Map<Integer, List<Integer>> graph;
23+
int nodeCnt;
24+
25+
int connectCnt;
26+
27+
List<List<Integer>> dependency;
28+
29+
// constructor
30+
public UnionFindTest1(Integer[] elements, List<List<Integer>> dependency, int nodeCnt){
31+
32+
// init parent
33+
this.parents = new Integer[nodeCnt];
34+
for(int i = 0; i < nodeCnt; i++){
35+
this.parents[i] = i; // at first, every element is its own parent
36+
}
37+
38+
// init graph
39+
this.dependency = dependency;
40+
this.graph = new HashMap<>();
41+
for(List<Integer> x: dependency){
42+
Integer parent = x.get(0);
43+
Integer child = x.get(1);
44+
45+
this.graph.putIfAbsent(parent, new ArrayList<>());
46+
47+
List<Integer> childList = this.graph.get(parent);
48+
childList.add(child);
49+
50+
this.graph.put(parent, childList); // ??
51+
}
52+
53+
// init nodecnt
54+
this.nodeCnt = nodeCnt;
55+
this.connectCnt = nodeCnt;
56+
}
57+
58+
// method
59+
/**
60+
* union
61+
*
62+
* find
63+
*
64+
* isConnected
65+
*/
66+
public void union(Integer node1, Integer node2){
67+
68+
if (node1.equals(node2)){
69+
return;
70+
}
71+
72+
Integer parent1 = this.find(node1);
73+
Integer parent2 = this.find(node2);
74+
75+
// can also do `path compression` for performance optimization
76+
this.connectCnt -= 1; // ??
77+
this.parents[node2] = parent1; // or this.parents[node1] = parent1
78+
}
79+
80+
// find node parent
81+
public Integer find(Integer node){
82+
83+
// ?
84+
if (this.parents[node].equals(node)){
85+
return node;
86+
}else{
87+
this.parents[node] = this.find(node);
88+
return this.parents[node]; // ??
89+
}
90+
//
91+
// if(this.graph.containsKey(node)){
92+
// for(int x: this.graph.get(node)){
93+
// this.find(x);
94+
// }
95+
// }
96+
//
97+
// return node; // ??
98+
}
99+
100+
public boolean isConnected(Integer node1, Integer node2){
101+
102+
if (node1.equals(node2)){
103+
return true;
104+
}
105+
return this.find(node1).equals(this.find(node2));
106+
}
107+
108+
public int connectCnt(){
109+
return this.connectCnt;
110+
}
111+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 136 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,85 +1839,165 @@ public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
18391839
* if not, return -1
18401840
* if yes, do the op, return res
18411841
*/
1842-
private class EquationRes{
1843-
// attr
1844-
String variable;
1845-
Double result;
18461842

1847-
public Double getResult() {
1848-
return result;
1849-
}
1843+
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
18501844

1851-
public String getVariable() {
1852-
return variable;
1845+
// edge
1846+
if(equations.isEmpty() || values.length == 0){
1847+
return null;
18531848
}
18541849

1855-
// constructor
1856-
EquationRes(String variable, Double result){
1857-
this.variable = variable;
1858-
this.result = result;
1859-
}
1860-
}
1850+
double[] res = new double[queries.size()]; // ?
18611851

1862-
// init relation
1863-
Map<String, List<EquationRes>> relations = new HashMap();
1864-
//double[] res = new double[];
1852+
// HashMap<String, HashMap<String, Double>> !!!!
1853+
// Map<String, List<Map<String, Double>>> graph = new HashMap<>();
1854+
Map<String, Map<String, Double>> graph = new HashMap<>();
1855+
for(int i = 0; i < equations.size(); i++){
1856+
List<String> eq = equations.get(i);
1857+
String firstVal = eq.get(0);
1858+
String secondVal = eq.get(1);
18651859

1866-
public double[] calcEquation(
1860+
graph.putIfAbsent(firstVal, new HashMap<>());
1861+
graph.putIfAbsent(secondVal, new HashMap<>());
18671862

1868-
List<List<String>> equations, double[] values, List<List<String>> queries) {
18691863

1870-
// build
1871-
buildRelation(equations, values);
1872-
// get
1873-
double[] res = new double[queries.size()];
1874-
for(int i = 0; i < queries.size(); i++){
1875-
res[i] = getResult(queries.get(i), 1);
1876-
}
1864+
Map<String, Double> cur = graph.get(firstVal);
1865+
cur.put(secondVal, values[i]);
18771866

1878-
System.out.println(">>> res = " + res);
1867+
Map<String, Double> cur2 = graph.get(firstVal);
1868+
cur2.put(secondVal, 1 / values[i]); // ???
18791869

1880-
return res;
1881-
}
1870+
graph.get(firstVal).put(firstVal, values[i]); // ??
1871+
graph.get(secondVal).put(secondVal, 1 / values[i]); // ??
18821872

1883-
// dfs
1884-
private double getResult(List<String> queries, double res){
1885-
// check if in list
1886-
String firstVal = queries.get(0);
1887-
String secondVal = queries.get(1);
1888-
if (!this.relations.containsKey(firstVal) || !this.relations.containsKey(secondVal)){
1889-
return -1.0;
1873+
//graph.get(1).put(1, 2);
18901874
}
18911875

1892-
//double res = 1;
1893-
//List<EquationRes> x = this.relations.get(firstVal);
1894-
for(EquationRes equationRes: this.relations.get(firstVal)){
1895-
res = res * equationRes.result;
1896-
1876+
// check if element in graph
1877+
for(int i = 0; i < equations.size(); i++){
1878+
List<String> q = equations.get(i);
1879+
String firstVal = q.get(0);
1880+
String secondVal = q.get(1);
1881+
if (!graph.containsKey(firstVal) || !graph.containsKey(secondVal)){
1882+
res[i] = -1.0;
1883+
}else if (firstVal.equals(secondVal)){
1884+
res[i] = 1.0; // ??
1885+
}
1886+
else{
1887+
// dfs call
1888+
double cur = 1.0;
1889+
Set<String> visited = new HashSet<>();
1890+
res[i] = cur;
1891+
}
18971892

18981893
}
18991894

19001895
return res;
1901-
}
1896+
}
19021897

1903-
// build relation
1904-
private void buildRelation(List<List<String>> equations, double[] values){
1905-
for(int i = 0; i < equations.size(); i++){
1906-
List<String> equation = equations.get(i);
1907-
String firstVal = equation.get(0);
1908-
String secondVal = equation.get(1);
1898+
private double dfsCal(Map<String, Map<String, Double>> graph, List<String> query, double cur, Set<String> visited){
19091899

1910-
EquationRes equationRes = new EquationRes(secondVal, values[i]);
1900+
String firstVal = query.get(0);
1901+
String secondVal = query.get(1);
19111902

1912-
List<EquationRes> equationAndRes = new ArrayList<>();
1913-
if (this.relations.containsKey(firstVal)){
1914-
equationAndRes = this.relations.get(firstVal);
1915-
}
1903+
if (firstVal.equals(secondVal)){
1904+
return 1.0;
1905+
}
19161906

1917-
this.relations.put(firstVal, equationAndRes);
1907+
if(graph.containsKey(firstVal) && graph.get(firstVal).containsKey(secondVal)){
1908+
cur = cur * graph.get(firstVal).get(secondVal);
1909+
//this.dfsCal(graph, query, cur, visited);
1910+
return cur;
19181911
}
19191912

1920-
}
1913+
if (graph.containsKey(firstVal) && !graph.get(firstVal).containsKey(secondVal)){
1914+
// for (String x: graph.get(firstVal)){
1915+
//
1916+
// }
1917+
}
1918+
1919+
return cur;
1920+
}
1921+
1922+
// private class EquationRes{
1923+
// // attr
1924+
// String variable;
1925+
// Double result;
1926+
//
1927+
// public Double getResult() {
1928+
// return result;
1929+
// }
1930+
//
1931+
// public String getVariable() {
1932+
// return variable;
1933+
// }
1934+
//
1935+
// // constructor
1936+
// EquationRes(String variable, Double result){
1937+
// this.variable = variable;
1938+
// this.result = result;
1939+
// }
1940+
// }
1941+
//
1942+
// // init relation
1943+
// Map<String, List<EquationRes>> relations = new HashMap();
1944+
// //double[] res = new double[];
1945+
//
1946+
// public double[] calcEquation(
1947+
//
1948+
// List<List<String>> equations, double[] values, List<List<String>> queries) {
1949+
//
1950+
// // build
1951+
// buildRelation(equations, values);
1952+
// // get
1953+
// double[] res = new double[queries.size()];
1954+
// for(int i = 0; i < queries.size(); i++){
1955+
// res[i] = getResult(queries.get(i), 1);
1956+
// }
1957+
//
1958+
// System.out.println(">>> res = " + res);
1959+
//
1960+
// return res;
1961+
// }
1962+
//
1963+
// // dfs
1964+
// private double getResult(List<String> queries, double res){
1965+
// // check if in list
1966+
// String firstVal = queries.get(0);
1967+
// String secondVal = queries.get(1);
1968+
// if (!this.relations.containsKey(firstVal) || !this.relations.containsKey(secondVal)){
1969+
// return -1.0;
1970+
// }
1971+
//
1972+
// //double res = 1;
1973+
// //List<EquationRes> x = this.relations.get(firstVal);
1974+
// for(EquationRes equationRes: this.relations.get(firstVal)){
1975+
// res = res * equationRes.result;
1976+
//
1977+
//
1978+
// }
1979+
//
1980+
// return res;
1981+
// }
1982+
//
1983+
// // build relation
1984+
// private void buildRelation(List<List<String>> equations, double[] values){
1985+
// for(int i = 0; i < equations.size(); i++){
1986+
// List<String> equation = equations.get(i);
1987+
// String firstVal = equation.get(0);
1988+
// String secondVal = equation.get(1);
1989+
//
1990+
// EquationRes equationRes = new EquationRes(secondVal, values[i]);
1991+
//
1992+
// List<EquationRes> equationAndRes = new ArrayList<>();
1993+
// if (this.relations.containsKey(firstVal)){
1994+
// equationAndRes = this.relations.get(firstVal);
1995+
// }
1996+
//
1997+
// this.relations.put(firstVal, equationAndRes);
1998+
// }
1999+
//
2000+
// }
19212001

19222002
// LC 1091
19232003
// 7.09 am - 7.15 am

0 commit comments

Comments
 (0)