[Programmers/프로그래머스] 2021 Dev-Matching: 웹 백엔드 개발자(상반기) [코딩테스트]
- [Lv. 1] 로또의 최고 순위와 최저 순위
- [Lv. 2] 행렬 테두리 회전하기
- [Lv. 3] 다단계 칫솔 판매
- [Lv. 3] 헤비 유저가 소유한 장소
from collections import defaultdict
def solution(enroll, referral, seller, amount):
tree = defaultdict(str) # 판매원 별 추천인 트리
for child, parent in zip(enroll, referral): # child, parent = 판매원, 추천인
tree[child] = parent
benefits = defaultdict(int)
amount = [num * 100 for num in amount] # 판매수량 당 이익은 100원이므로 * 100
for node, price in zip(seller, amount): # 각 판매원 별 판매금액에 대하여
while True: # root 노드까지 탐색을 위한 while문
if price < 10: # 금액이 10원단위 미만이면
benefits[node] += price # 현재노드에 최종이익 추가 후
break # 부모노드 탐색종료
else: # 금액이 10원단위 이상이면
benefits[node] += price - int(price * 0.1) # 금액의 10%를 뺀 나머지 금액 합산
if tree[node] == '-': # 부모노드가 민호이면
break # 부모노드 탐색종료
node = tree[node] # 부모노드
price = int(price * 0.1) # 부모노드로 전달할 현재금액의 10%
return [benefits[name] for name in enroll] # enroll 순서대로 이익금 반환