[프로그래머스 Lv. 3] 다단계 칫솔 판매

DaeHoon·2021년 10월 13일
0

프로그래머스

목록 보기
8/16

문제

https://programmers.co.kr/learn/courses/30/lessons/77486

접근

1) 조직원 간 관계를 나타내는 people과 판매원들이 벌은 돈을 담은 moneys를 딕셔너리로 둔다.

2) 현재 벌 수 있는 돈 curr_fee와 현재 판매원인 curr_seller 변수를 만들어 이익이 0이 되거나 순서가 민호에게 올라올 때 까지 moneys 딕셔너리를 업데이트 한다.

3) enroll에 있는 사람 순서대로 answer에 값을 넣어준다.

Code

from collections import defaultdict


def solution(enroll, referral, seller, amount):
    answer = []
    people = defaultdict(list)
    moneys = defaultdict(int)
    for i in range(len(enroll)):
        people[enroll[i]] = referral[i]

    for i in range(len(seller)):
        curr_fee = amount[i] * 100
        minus = curr_fee
        curr_person = seller[i]
        while True:
            if curr_person == '-' or minus == 0:
                break
            minus = (curr_fee * 10) // 100

            moneys[curr_person] += curr_fee - minus
            curr_fee = minus
            curr_person = people[curr_person]
    for i in enroll:
        answer.append(moneys[i])
    return answer

여담

보통 트리 문제는 위에서 아래로 가지만, 이 문제는 특이하게 아래서부터 위로 접근하는 문제였다. 현실 고증이 정말 잘 된 문제같다.

profile
평범한 백엔드 개발자

0개의 댓글