LV 3: 다단계 칫솔 판매

ewillwin·2023년 9월 4일
0

문제 링크

LV 3: 다단계 칫솔 판매


구현 방식

  • 전반적으로 find-union 형식으로 구현해주었다

  • graph는 child를 key로, parent를 value로 설정하여 dictionary로 선언해주었고, all_profit 역시 dictionary로 선언해주었다

  • distribute(node, profit) 함수에서 재귀 호출을 할 때, TC 11번 ~ 13번이 런타임 에러가 났는데, recursion limit exceeded가 뜬 것으로 추정된다

    • 이 부분은 profit // 10이 0일 때 더 이상 진행하지 않도록 조건문을 추가하여 해결해주었다

코드

def solution(enroll, referral, seller, amount):
    N = len(enroll)
    
    graph = dict() #key:child, value:parent
    for i in range(N): graph[enroll[i]] = referral[i]
        
    all_profit = dict()
    for i in range(N): all_profit[enroll[i]] = 0
        
    def distribute(node, profit):
        if profit // 10 == 0:
            all_profit[node] += profit
            return
        
        tax = profit // 10
        all_profit[node] += profit - tax
        
        if graph[node] != '-':
            distribute(graph[node], tax)

    M = len(seller)
    for i in range(M): distribute(seller[i], amount[i]*100)
    
    return list(all_profit.values())
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글