-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
34 lines (26 loc) · 809 Bytes
/
task3.py
File metadata and controls
34 lines (26 loc) · 809 Bytes
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
from collections import defaultdict
def solution(S, A):
n = len(S)
tree = defaultdict(list)
root = -1
# 트리 만들기
for child in range(n):
parent = S[child]
if parent == -1:
root = child
else:
tree[parent].append(child)
def dfs(node):
valid_lengths = []
for child in tree[node]:
child_len = dfs(child)
# 부모-자식 문자가 다르면 연결 가능
if A[child] != A[node]:
valid_lengths.append(child_len)
if not valid_lengths:
return 1 # 자기 자신만 있는 경로 길이
# 가장 긴 경로로만 이어감
return max(valid_lengths) + 1
return dfs(-1)
# 테스트
print(solution([-1,0], "ab")) # 예상: 3