목차

다단계 칫솔 판매

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호77486
문제명다단계 칫솔 판매
레벨Level 3
분류

트리

시간복잡도O(n+m)
인풋사이즈n<=10000, m<=100000
사용한 언어Python
해결날짜2021/07/06
출처

ps:problems:programmers:2021_dev-matching_-_웹_백엔드_개발자_상반기

풀이

코드

"""Solution code for "Programmers 77486. 다단계 칫솔 판매".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/77486
- Solution link: http://www.teferi.net/ps/problems/programmers/77486
"""

import collections

COST = 100


def solution(enroll, referral, seller, amount):
    parents = {child: parent for child, parent in zip(enroll, referral)}
    parents['-'] = '-'
    profits = collections.defaultdict(int)

    for s, a in zip(seller, amount):
        profit = a * COST
        member = s
        while profit > 0:
            profit_to_referral = profit // 10
            profits[member] += profit - profit_to_referral
            profit, member = profit_to_referral, parents[member]

    return [profits[x] for x in enroll]