[알고리즘] 프로그래머스 - 다단계 칫솔 판매

June·2021년 9월 7일
0

알고리즘

목록 보기
249/260

프로그래머스 - 다단계 칫솔 판매

내 풀이

import math
from collections import defaultdict

def solution(enroll, referral, seller, amount):
    answer = []
    graph_dict = dict()
    profit_dict = defaultdict(int)

    for i in range(len(enroll)):
        graph_dict[enroll[i]] = referral[i]

    for i in range(len(seller)):
        cur_person, cur_amount = seller[i], amount[i]
        cur_money = cur_amount * 100

        while cur_person != "-":
            lost_profit = math.floor(cur_money * 0.1)
            my_profit = cur_money - lost_profit
            if my_profit == 0:
                break
            profit_dict[cur_person] += my_profit
            cur_money = lost_profit
            cur_person = graph_dict[cur_person]

    for person in enroll:
        answer.append(profit_dict[person])

    return answer

실제 문제를 풀 때는 이것보다 난이도가 높았던 것 같은데 다시 푸니 쉬운 편이다. 다만 소수점 계산 관련해서 조심해야 한다. 10퍼센트를 주느냐, 90퍼센트를 가지고 전체에서 뺴고 주냐에서 답이 차이가 난다. 소수점 버림에서 차이가 나기 때문이다.

0개의 댓글

관련 채용 정보