forked from sherxon/AlgoDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveCircus.java
More file actions
115 lines (102 loc) · 3.33 KB
/
Copy pathRecursiveCircus.java
File metadata and controls
115 lines (102 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package adventofcode;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
@SuppressWarnings("Duplicates")
public class RecursiveCircus {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> list = new ArrayList<>();
while (scanner.hasNextLine()) {
try {
String s = scanner.nextLine();
if (s.equals("1"))
break;
list.add(s);
} catch (Exception e) {
break;
}
}
System.out.println(solve2(list));
}
private static String solve(List<String> list) {
if (list == null || list.isEmpty())
return "";
Map<String, String> map = new HashMap<>();
String ss = "";
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String[] a = s.split(" ");
ss = a[0];
if (a.length > 3) {
for (int j = 3; j < a.length; j++) {
if (a[j].endsWith(","))
map.put(a[j].substring(0, a[j].length() - 1), a[0]);
else
map.put(a[j], a[0]);
}
}
}
while (map.get(ss) != null) {
ss = map.get(ss);
}
return ss;
}
private static Integer solve2(List<String> list) {
if (list == null || list.isEmpty())
return 0;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String[] a = s.split(" ");
map.put(a[0], Integer.parseInt(a[1].substring(1, a[1].length() - 1)));
}
Map<String, Set<String>> tree = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String[] a = s.split(" ");
if (!tree.containsKey(a[0]))
tree.put(a[0], new HashSet<>());
for (int j = 3; j < a.length; j++) {
String child = a[j];
if (child.endsWith(","))
child = a[j].substring(0, a[j].length() - 1);
tree.get(a[0]).add(child);
}
}
String root = solve(list);
go(tree, root, map);
return res;
}
static int res = -1;
private static int go(Map<String, Set<String>> map, String root, Map<String, Integer> values) {
if (!map.containsKey(root))
return 0;
if (map.get(root).size() == 0)
return values.get(root);
int sum = values.get(root);
Map<Integer, Integer> counts = new HashMap<>();
Map<Integer, String> kk = new HashMap<>();
for (String s : map.get(root)) {
int r = go(map, s, values);
kk.put(r, s);
counts.put(r, counts.getOrDefault(r, 0) + 1);
sum += r;
}
if (counts.size() == 1)
return sum;
int one = 1;
int more = 1;
for (Integer key : counts.keySet()) {
if (counts.get(key) == 1) {
one = key;
} else {
more = key;
}
}
if (res == -1)
res = values.get(kk.get(one)) - (one - more);
return sum;
}
}