[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

2021. 7. 16. 18:00ใ†ETC/Algorithm

๐Ÿ‘Š๐Ÿป ๋ฌธ์ œ

๋ฏผํ˜ธ๋Š” ๋‹ค๋‹จ๊ณ„ ์กฐ์ง์„ ์ด์šฉํ•˜์—ฌ ์นซ์†”์„ ํŒ๋งคํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŒ๋งค์›์ด ์นซ์†”์„ ํŒ๋งคํ•˜๋ฉด ๊ทธ ์ด์ต์ด ํ”ผ๋ผ๋ฏธ๋“œ ์กฐ์ง์„ ํƒ€๊ณ  ์กฐ๊ธˆ์”ฉ ๋ถ„๋ฐฐ๋˜๋Š” ํ˜•ํƒœ์˜ ํŒ๋งค๋ง์ž…๋‹ˆ๋‹ค. ์–ด๋Š์ •๋„ ํŒ๋งค๊ฐ€ ์ด๋ฃจ์–ด์ง„ ํ›„, ์กฐ์ง์„ ์šด์˜ํ•˜๋˜ ๋ฏผํ˜ธ๋Š” ์กฐ์ง ๋‚ด ๋ˆ„๊ฐ€ ์–ผ๋งˆ๋งŒํผ์˜ ์ด๋“์„ ๊ฐ€์ ธ๊ฐ”๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฏผํ˜ธ๊ฐ€ ์šด์˜ํ•˜๊ณ  ์žˆ๋Š” ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค ์กฐ์ง์ด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.

๋ฏผํ˜ธ๋Š” center์ด๋ฉฐ, ํŒŒ๋ž€์ƒ‰ ๋„ค๋ชจ๋Š” ์—ฌ๋Ÿ ๋ช…์˜ ํŒ๋งค์›์„ ํ‘œ์‹œํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์€ ์ž์‹ ์„ ์กฐ์ง์— ์ฐธ์—ฌ์‹œํ‚จ ์ถ”์ฒœ์ธ์— ์—ฐ๊ฒฐ๋˜์–ด ํ”ผ๋ผ๋ฏธ๋“œ ์‹์˜ ๊ตฌ์กฐ๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ์ง์˜ ์ด์ต ๋ถ„๋ฐฐ ๊ทœ์น™์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ํŒ๋งค์›์€ ์นซ์†”์˜ ํŒ๋งค์— ์˜ํ•˜์—ฌ ๋ฐœ์ƒํ•˜๋Š” ์ด์ต์—์„œ 10% ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์ž์‹ ์„ ์กฐ์ง์— ์ฐธ์—ฌ์‹œํ‚จ ์ถ”์ฒœ์ธ์—๊ฒŒ ๋ฐฐ๋ถ„ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์ž์‹ ์ด ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  ํŒ๋งค์›์€ ์ž์‹ ์ด ์นซ์†” ํŒ๋งค์—์„œ ๋ฐœ์ƒํ•œ ์ด์ต ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ž์‹ ์ด ์กฐ์ง์— ์ถ”์ฒœํ•˜์—ฌ ๊ฐ€์ž…์‹œํ‚จ ํŒ๋งค์›์—๊ฒŒ์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด์ต์˜ 10% ๊นŒ์ง€ ์ž์‹ ์— ์ด์ต์ด ๋ฉ๋‹ˆ๋‹ค. ์ž์‹ ์—๊ฒŒ ๋ฐœ์ƒํ•˜๋Š” ์ด์ต ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€์˜ ๊ทœ์น™์œผ๋กœ ์ž์‹ ์˜ ์ถ”์ฒœ์ธ์—๊ฒŒ ๋ถ„๋ฐฐ๋ฉ๋‹ˆ๋‹ค. ๋‹จ, 10% ๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ์—๋Š” ์› ๋‹จ์œ„์—์„œ ์ ˆ์‚ฌํ•˜๋ฉฐ, 10%๋ฅผ ๊ณ„์‚ฐํ•œ ๊ธˆ์•ก์ด 1 ์› ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ์—๋Š” ์ด๋“์„ ๋ถ„๋ฐฐํ•˜์ง€ ์•Š๊ณ  ์ž์‹ ์ด ๋ชจ๋‘ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 

์ œํ•œ์‚ฌํ•ญ

  • enroll์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • enroll์— ๋ฏผํ˜ธ์˜ ์ด๋ฆ„์€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ enroll์˜ ๊ธธ์ด๋Š” ๋ฏผํ˜ธ๋ฅผ ์ œ์™ธํ•œ ์กฐ์ง ๊ตฌ์„ฑ์›์˜ ์ด ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • referral์˜ ๊ธธ์ด๋Š” enroll์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • referral ๋‚ด์—์„œ i ๋ฒˆ์งธ์— ์žˆ๋Š” ์ด๋ฆ„์€ ๋ฐฐ์—ด enroll ๋‚ด์—์„œ i ๋ฒˆ์งธ์— ์žˆ๋Š” ํŒ๋งค์›์„ ์กฐ์ง์— ์ฐธ์—ฌ์‹œํ‚จ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.
    • ์–ด๋Š ๋ˆ„๊ตฌ์˜ ์ถ”์ฒœ๋„ ์—†์ด ์กฐ์ง์— ์ฐธ์—ฌํ•œ ์‚ฌ๋žŒ์— ๋Œ€ํ•ด์„œ๋Š” referral ๋ฐฐ์—ด ๋‚ด์— ์ถ”์ฒœ์ธ์˜ ์ด๋ฆ„์ด ๊ธฐ์ž…๋˜์ง€ ์•Š๊ณ  “-“ ๊ฐ€ ๊ธฐ์ž…๋ฉ๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” john ๊ณผ mary ๊ฐ€ ์ด๋Ÿฌํ•œ ์˜ˆ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
    • enroll ์— ๋“ฑ์žฅํ•˜๋Š” ์ด๋ฆ„์€ ์กฐ์ง์— ์ฐธ์—ฌํ•œ ์ˆœ์„œ์— ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
    • ์ฆ‰, ์–ด๋Š ํŒ๋งค์›์˜ ์ด๋ฆ„์ด enroll ์˜ i ๋ฒˆ์งธ์— ๋“ฑ์žฅํ•œ๋‹ค๋ฉด, ์ด ํŒ๋งค์›์„ ์กฐ์ง์— ์ฐธ์—ฌ์‹œํ‚จ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„, ์ฆ‰ referral ์˜ i ๋ฒˆ์งธ ์›์†Œ๋Š” ์ด๋ฏธ ๋ฐฐ์—ด enroll ์˜ j ๋ฒˆ์งธ (j < i) ์— ๋“ฑ์žฅํ–ˆ์Œ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.
  • seller์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • seller ๋‚ด์˜ i ๋ฒˆ์งธ์— ์žˆ๋Š” ์ด๋ฆ„์€ i ๋ฒˆ์งธ ํŒ๋งค ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋Š ํŒ๋งค์›์— ์˜ํ•œ ๊ฒƒ์ธ์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • seller ์—๋Š” ๊ฐ™์€ ์ด๋ฆ„์ด ์ค‘๋ณตํ•ด์„œ ๋“ค์–ด์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • amount์˜ ๊ธธ์ด๋Š” seller์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • amount ๋‚ด์˜ i ๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋Š” i ๋ฒˆ์งธ ํŒ๋งค ์ง‘๊ณ„ ๋ฐ์ดํ„ฐ์˜ ํŒ๋งค๋Ÿ‰์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ํŒ๋งค๋Ÿ‰์˜ ๋ฒ”์œ„, ์ฆ‰ amount ์˜ ์›์†Œ๋“ค์˜ ๋ฒ”์œ„๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ์นซ์†” ํ•œ ๊ฐœ๋ฅผ ํŒ๋งคํ•˜์—ฌ ์–ป์–ด์ง€๋Š” ์ด์ต์€ 100 ์›์œผ๋กœ ์ •ํ•ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์กฐ์ง ๊ตฌ์„ฑ์›๋“ค์˜ ์ด๋ฆ„์€ 10 ๊ธ€์ž ์ด๋‚ด์˜ ์˜๋ฌธ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

enroll referral seller amount result
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["young", "john", "tod", "emily", "mary"] [12, 4, 2, 5, 10] [360, 958, 108, 0, 450, 18, 180, 1080]
["john", "mary", "edward", "sam", "emily", "jaimie", "tod", "young"] ["-", "-", "mary", "edward", "mary", "mary", "jaimie", "edward"] ["sam", "emily", "jaimie", "edward"] [2, 3, 5, 4] [0, 110, 378, 180, 270, 450, 0, 0]

 

 

๐ŸŽƒ ๋ฌธ์ œ ํ•ด๊ฒฐ

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

โœ”๏ธ enroll์„ key๋กœ, referral์„ value ๋กœ ๊ฐ–๋Š” map ์ƒ์„ฑ

โœ”๏ธ ๊ฐ enroll์˜ ์ˆ˜์ต๊ธˆ์„ ์ €์žฅํ•  ๋ฐฐ์—ด p๋ฅผ ์ƒ์„ฑ ํ›„  0์œผ๋กœ ์ดˆ๊ธฐํ™”

โœ”๏ธ center์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋ถ€๋ชจ์—๊ฒŒ ์ˆ˜์ต๊ธˆ์„ ๋ฐฐ๋ถ„ํ•  ์žฌ๊ท€ ํ•จ์ˆ˜ ์ƒ์„ฑ

โœ”๏ธ center์ด ๋‚˜์˜ค๊ฑฐ๋‚˜ ๋ฐฐ๋ถ„ํ•  ๊ธˆ์•ก์ด ์—†์œผ๋ฉด ์žฌ๊ท€๋ฅผ ํƒˆ์ถœ

 

 

์ฝ”๋“œ

#include <vector>
#include <map>

using namespace std;

void checkRef(map<string, string> &m, map<string, int> &p, string enroll, int price) {
    if (price < 1) return;
    if (enroll == "-") {
        p[enroll] += price;
        return;
    }
    p[enroll] += price - (int)(price * 0.1);
    checkRef(m, p, m[enroll], price * 0.1);
}

vector<int> solution(vector<string> enroll, vector<string> referral, vector<string> seller, vector<int> amount) {
    vector<int> answer;
    map<string, string> m;
    map<string, int> p;
    
    for (int i = 0; i < enroll.size(); i++) {
        m[enroll[i]] = referral[i];
        p[enroll[i]] = 0;
    }
    
    for (int s=0; s < seller.size(); s++)
        checkRef(m, p, seller[s], amount[s] * 100);
    
    for (string e : enroll)
        answer.push_back(p[e]);
    
    return answer;
}