@@ -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