๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

programmers. ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

ttoance 2022. 6. 29. 01:24
 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

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

programmers.co.kr

๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

 

๋‚ด ํ’€์ด  (0.5H)

import java.util.HashMap;

class Solution {
    static HashMap<String, String> relationMaps = new HashMap<String, String>();
    static HashMap<String, Integer> payMaps = new HashMap<String, Integer>();
    
    public int[] solution(String[] enrolls, String[] referrals, String[] sellers, int[] amounts) {
        int[] answer = new int[enrolls.length];
        
        // ๊ด€๊ณ„ Map ํ˜•์‹์œผ๋กœ ๋งคํ•‘ 
        for (int i = 0; i < enrolls.length; i++) {
            relationMaps.put(enrolls[i], referrals[i]);
        }
        
        // 0์œผ๋กœ ์ดˆ๊ธฐํ™” 
        for (int i = 0; i < enrolls.length; i++) {
            payMaps.put(enrolls[i], 0);
        }
        
        // ์ˆ˜์ต ๊ณ„์‚ฐ 
        for (int i = 0; i < sellers.length; i++) {
            getPaid(sellers[i], amounts[i] * 100);
        }
        
        for (int i = 0; i < enrolls.length; i++) {
            answer[i] = payMaps.get(enrolls[i]);
        }
              
        return answer;
    }
    
    public void getPaid(String seller, int earns) {
        int referralPaid = (int) (earns * 0.1);
        int sellerPaid = earns - referralPaid;
        
        payMaps.replace(seller, payMaps.get(seller) + sellerPaid); // debug # 1 : ๋จผ์ € ์ง€๊ธ‰ํ•ด์•ผํ•จ 
        
        if (relationMaps.get(seller).equals("-")) {
            return; 
        }
        if (referralPaid < 1) {
            return; 
        }
    
        getPaid(relationMaps.get(seller), referralPaid);
    }
}

1. debug #1 : ํ˜ธ์ถœ ์ข…๋ฃŒ ํ›„ ์ง€๊ธ‰์ด ์•„๋‹Œ ์ง€๊ธ‰ํ›„ ์ข…๋ฃŒ ํ–ˆ์–ด์•ผํ•จ

 

๋ฐ˜์‘ํ˜•