Skip to content

Commit 02dd8b6

Browse files
committed
fd
1 parent e40943c commit 02dd8b6

File tree

3 files changed

+55
-72
lines changed

3 files changed

+55
-72
lines changed

solution/src/main/java/com/inuker/solution/EvaluateDivision.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,39 @@ public double[] calcEquation(String[][] equations, double[] values, String[][] q
1919

2020
for (int i = 0; i < equations.length; i++) {
2121
String[] equation = equations[i];
22-
HashMap<String, Double> map = valueMap.get(equation[0]);
23-
if (map == null) {
24-
map = new HashMap<>();
25-
valueMap.put(equation[0], map);
26-
}
22+
HashMap<String, Double> map = valueMap.computeIfAbsent(equation[0], k -> new HashMap<>());
2723
map.put(equation[1], values[i]);
28-
29-
map = valueMap.get(equation[1]);
30-
if (map == null) {
31-
map = new HashMap<>();
32-
valueMap.put(equation[1], map);
33-
}
24+
map = valueMap.computeIfAbsent(equation[1], k -> new HashMap<>());
3425
map.put(equation[0], 1 / values[i]);
3526
}
3627

3728
double[] result = new double[queries.length];
3829
for (int i = 0; i < queries.length; i++) {
39-
double res = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<String>(), 1.0);
40-
if (res == 0.0) {
41-
result[i] = -1.0;
42-
} else {
43-
result[i] = res;
44-
}
30+
double[] value = new double[] {1.0};
31+
result[i] = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<>(), value) ? value[0] : -1.0;
4532
}
4633
return result;
4734
}
4835

49-
private double dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double value) {
50-
if (set.contains(start)) {
51-
return 0.0;
52-
}
53-
if (!map.containsKey(start) || !map.containsKey(end)) {
54-
return 0.0;
36+
private boolean dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double[] value) {
37+
if (!map.containsKey(start) || !map.containsKey(end) || set.contains(start)) {
38+
return false;
5539
}
5640
if (start.equals(end)) {
57-
return value;
41+
return true;
5842
}
5943
set.add(start);
60-
61-
double res = 0.0;
6244
HashMap<String, Double> valueMap = map.get(start);
45+
boolean flag = false;
6346
for (Map.Entry<String, Double> entry : valueMap.entrySet()) {
64-
res = dfs(map, entry.getKey(), end, set, value * entry.getValue());
65-
if (res > 0) {
47+
value[0] *= entry.getValue();
48+
if (dfs(map, entry.getKey(), end, set, value)) {
49+
flag = true;
6650
break;
6751
}
52+
value[0] /= entry.getValue();
6853
}
6954
set.remove(start);
70-
return res;
55+
return flag;
7156
}
7257
}

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,44 @@
1919

2020
public class Test2 {
2121

22-
public List<String> findItinerary(String[][] tickets) {
23-
HashMap<String, ArrayList<String>> map = new HashMap<>();
24-
for (String[] ticket : tickets) {
25-
map.computeIfAbsent(ticket[0], k -> new ArrayList<>()).add(ticket[1]);
22+
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
23+
HashMap<String, HashMap<String, Double>> valueMap = new HashMap<>();
24+
25+
for (int i = 0; i < equations.length; i++) {
26+
String[] equation = equations[i];
27+
HashMap<String, Double> map = valueMap.computeIfAbsent(equation[0], k -> new HashMap<>());
28+
map.put(equation[1], values[i]);
29+
map = valueMap.computeIfAbsent(equation[1], k -> new HashMap<>());
30+
map.put(equation[0], 1 / values[i]);
2631
}
27-
for (List<String> list : map.values()) {
28-
Collections.sort(list);
32+
33+
double[] result = new double[queries.length];
34+
for (int i = 0; i < queries.length; i++) {
35+
double[] value = new double[] {1.0};
36+
result[i] = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<>(), value) ? value[0] : -1.0;
2937
}
30-
LinkedList<String> result = new LinkedList<>(Arrays.asList("JFK"));
31-
dfs("JFK", map, result, tickets.length);
3238
return result;
3339
}
3440

35-
boolean dfs(String airport, HashMap<String, ArrayList<String>> map, LinkedList<String> route, int n) {
36-
if (route.size() == n + 1) {
37-
return true;
38-
}
39-
if (!map.containsKey(airport) || map.get(airport).isEmpty()) {
41+
private boolean dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double[] value) {
42+
if (!map.containsKey(start) || !map.containsKey(end) || set.contains(start)) {
4043
return false;
4144
}
42-
ArrayList<String> list = map.get(airport);
43-
for (int i = 0; i < list.size(); i++) {
44-
String target = list.remove(i);
45-
46-
route.add(target);
47-
if (dfs(target, map, route, n)) {
48-
return true;
45+
if (start.equals(end)) {
46+
return true;
47+
}
48+
set.add(start);
49+
HashMap<String, Double> valueMap = map.get(start);
50+
boolean flag = false;
51+
for (Map.Entry<String, Double> entry : valueMap.entrySet()) {
52+
value[0] *= entry.getValue();
53+
if (dfs(map, entry.getKey(), end, set, value)) {
54+
flag = true;
55+
break;
4956
}
50-
route.removeLast();
51-
52-
list.add(i, target);
57+
value[0] /= entry.getValue();
5358
}
54-
return false;
59+
set.remove(start);
60+
return flag;
5561
}
5662
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,17 @@
2626
public class main {
2727

2828
public static void main(String[] args) {
29-
String[][] strs = new String[][]{
30-
// {"MUC", "LHR"},
31-
// {"JFK", "MUC"},
32-
// {"SFO", "SJC"},
33-
// {"LHR", "SFO"}
34-
{"JFK", "SFO"},
35-
{"JFK", "ATL"},
36-
{"SFO", "ATL"},
37-
{"ATL", "JFK"},
38-
{"ATL", "SFO"},
39-
};
40-
List<String> lists = new Test2().findItinerary(strs);
41-
// List<String> lists = new Solution().findItinerary(strs);
42-
// for (String s : lists) {
43-
// System.out.print(s + " ");
44-
// }
45-
46-
for (String s : lists) {
47-
System.out.print(s + " ");
29+
double[] result = new Test2().calcEquation(new String[][] {
30+
{"a", "b"},
31+
{"b", "c"}
32+
}, new double[] {
33+
2.0f, 3.0f
34+
}, new String[][] {
35+
{"a", "c"},
36+
{"b", "c"}
37+
});
38+
for (double f : result) {
39+
System.out.print(f + " ");
4840
}
4941
}
5042
}

0 commit comments

Comments
 (0)