回答編集履歴

1

追記

2019/12/18 10:34

投稿

quickquip
quickquip

スコア11042

test CHANGED
@@ -1,3 +1,65 @@
1
+ (追記)
2
+
3
+ もっと素直に組んでみました
4
+
5
+ ```python
6
+
7
+ def unify_group2(all_groups: List[Set[int]]) -> List[Set[int]]:
8
+
9
+ val_to_index = {}
10
+
11
+ result = []
12
+
13
+
14
+
15
+ for s in all_groups:
16
+
17
+ indices = {val_to_index.get(n, -1) for n in s}
18
+
19
+ if max(indices) == -1:
20
+
21
+ result.append(s)
22
+
23
+ index = len(result) - 1
24
+
25
+ for n in s:
26
+
27
+ val_to_index[n] = index
28
+
29
+ else:
30
+
31
+ indices = sorted(i for i in indices if i > -1)
32
+
33
+ index = indices[0]
34
+
35
+ for i in indices[1:]:
36
+
37
+ result[index] |= result[i]
38
+
39
+ for j in result[i]:
40
+
41
+ val_to_index[j] = index
42
+
43
+ # 空をセットしておいて後で取り除く
44
+
45
+ result[i] = {}
46
+
47
+ result[index] |= s
48
+
49
+ for n in s:
50
+
51
+ val_to_index[n] = index
52
+
53
+
54
+
55
+ return list(filter(None, result))
56
+
57
+ ```
58
+
59
+ ----
60
+
61
+
62
+
1
63
  私だったら……
2
64
 
3
65
  ```python