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

Eden·2023년 7월 17일
0

문제 바로가기https://school.programmers.co.kr/learn/courses/30/lessons/77486

using System;
using System.Collections.Generic; //딕셔너리 사용
public class Solution {
    Dictionary<int, int> tree = new Dictionary<int, int>(); // <부사수 인덱스, 사수 인덱스> 딕셔너리
    Dictionary<string, int> nameidx = new Dictionary<string, int>(); //enroll에 대한 이름과 인덱스 딕셔너리
    int[] answer = new int[0];
    public int[] solution(string[] enroll, string[] referral, string[] seller, int[] amount ) {
        answer = new int[enroll.Length];
        for (int i = 0; i< enroll.Length; i++) // 딕셔너리 정의
        {
            nameidx[enroll[i]] = i;
            if (referral[i] != "-")
                tree[i] = Array.IndexOf(enroll, referral[i]);
            else
                tree[i] = -1; // 사수가 없는 값 -1
        }

        for (int i = 0; i < amount.Length; i++)
        {
            Calprice(nameidx[seller[i]], amount[i] * 100);
        }

        return answer;
    }
    public void Calprice(int name, int mount)
    {
        int price = (int)Math.Ceiling(mount * 0.9);
        answer[name] += price;
        if (tree[name] == -1)
        {
            return;
        }
        else
        {
            if (mount * 0.1 >= 1) // 사수가 존재하고 수수료가 나올 수 있는 금액이면 재귀
            Calprice(tree[name], mount - price);
            else // 사수가 가져가는 돈에서 더이상 수수료가 나올 수 없으면 사수에게 수수료를 할당하고 끝
                tree[name] += mount - price;

        }
    }
}
profile
주섬주섬..

0개의 댓글