https://programmers.co.kr/learn/courses/30/lessons/77486
Code
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
from dataclasses import dataclass
def solution(enroll, referral, seller, amount):
@dataclass
class Node:
name : str
parent : None
children: None
profit = 0
nodes = {name: Node(name, parent, []) for name, parent in zip(enroll, referral)}
nodes.update({'-': Node('-', None, [])})
for name, node in nodes.items(): # sorted
if node.parent is not None:
nodes[node.parent].children.append(name)
for name, val in zip(seller, amount):
val *= 100
cur_node = nodes[name]
while True:
cur_profit = val - int(val * 0.1)
cur_node.profit += cur_profit
val -= cur_profit
if val == 0:
break
elif cur_node.parent == '-':
nodes[cur_node.parent].profit += val
break
cur_node = nodes[cur_node.parent]
return [node.profit for node in nodes.values()][:-1]
Complexity
$O(|\text{enroll}| \times |\text{seller}|)$
PREVIOUSEtc