Search
Duplicate

다단계 칫솔 판매

문제 설명 및 제한사항

아이디어 및 해결 방법

코드

def propagate(u, am, p, val): if p[u] == u: val[u] += am return if am >= 10: val[u] += am - int(am * 0.1) propagate(p[u], int(am * 0.1), p, val) else: val[u] += am def solution(enroll, referral, seller, amount): name2i = {name:i for i, name in enumerate(enroll, 1)} enroll = [name2i[name] for name in enroll] referral = [name2i.get(name, 0) for name in referral] seller = [name2i[name] for name in seller] p = [0] * (len(enroll) + 1) val = [0] * (len(enroll) + 1) for u, v in zip(enroll, referral): p[u] = v for u, am in zip(seller, amount): propagate(u, am * 100, p, val) return val[1:]
Python
복사

출처

프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/challenges