[Programmers/프로그래머스] 2021 Dev-Matching: 웹 백엔드 개발자(상반기) 다단계 칫솔 판매 - Python/파이썬 [해설/풀이]

SihoonCho·2022년 9월 29일
0
post-thumbnail
[Programmers/프로그래머스] 2021 Dev-Matching: 웹 백엔드 개발자(상반기) [코딩테스트]
  1. [Lv. 1] 로또의 최고 순위와 최저 순위
  2. [Lv. 2] 행렬 테두리 회전하기
  3. [Lv. 3] 다단계 칫솔 판매
  4. [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 순서대로 이익금 반환
profile
개발을 즐길 줄 아는 백엔드 개발자

0개의 댓글