Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

프로그래머스 LV3 다단계 칫솔판매 본문

알고리즘

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

refindmySapporo 2024. 3. 19. 11:36
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/77486

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제가 트리로 풀어야 하나? 라는 생각이 들었지만

그래프 노드를 만들고 그냥 문제풀이처럼 부모노드를 탐색하는 방식을 활용하면 되었다.


조금 까다로웠던 건 10% 이익를 부모 노드가 가지는 것인데 그렇게 되면 90%의 이익을 자식노드가 가지는 것이 아닌 원금에서 10%를 뺀 가격을 추가해줘야 했다.

import java.util.*;
class Solution {
    public int[] solution(String[] enroll, String[] referral, String[] seller, int[] amount)     {
        String [] arr = new String[enroll.length];
        HashMap<String,String> hm = new HashMap<>();
        HashMap<String,Integer> money = new HashMap<>();
        int t = 0;
        for (String e : enroll) {
            arr[t] = e;
            hm.put(e,null);
            money.put(e,0);
            t++;
        }
        t = 0;
        for (String x : referral) {
            if (!x.equals("-")) {
                hm.put(arr[t],x);
            }
            t++;
        }
        
        for (int i = 0; i < seller.length; i++) {
            String s = seller[i];
            String c = hm.get(s);
            int m = amount[i] * 100;
            int up = m * 10 / 100;                
            money.put(s,money.get(s)+ m- up);
            while(c != null) {
              if(up > 0) {
                money.put(c,money.get(c)+  up - up * 10 / 100);   
                up = up * 10 / 100;   
                c = hm.get(c);
              }
              else {
                break;   
              }
            }
            // System.out.println(money.toString());
            
        }
        int [] answer = new int[enroll.length];
        for (int i = 0; i < arr.length; i++) {
            answer[i] = money.get(arr[i]);
        }
        return answer;
    }
}
반응형