Programers : 다단계 칫솔 판매 - C++

김정욱·2021년 4월 28일
0

Algorithm - 문제

목록 보기
242/249
post-custom-banner

다단계 칫솔 판매

코드

#include <string>
#include <vector>
#include <map>
using namespace std;
// 0108 ~ 0135
vector<int> solution(vector<string> enroll, vector<string> referral, vector<string> seller, vector<int> amount) {
    vector<int> answer(enroll.size());
    map<string,string> parent;
    map<string,int> number_map;
    for(int i=0;i<referral.size();i++)
    {
        parent[enroll[i]] = referral[i];
        number_map[enroll[i]] = i;
    }
    for(int i=0;i<seller.size();i++)
    {
        int price = 100 * amount[i];
        int part = price/10;
        string next = parent[seller[i]];
        int n = number_map[seller[i]];
        answer[n] += price - part;
        price = part;
        part = price/10;
        while(price > 0 and next != "-")
        {
            n = number_map[next];
            answer[n] += price - part;
            price = part;
            part = price/10;
            next = parent[next];
        }
    }
    return answer;
}
  • 핵심
    • map을 이용해서 parent인덱스관리해서 문제 해결
profile
Developer & PhotoGrapher
post-custom-banner

0개의 댓글