개발의변화
프로그래머스 LV3 다단계 칫솔판매 본문
반응형
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;
}
}
반응형
'알고리즘' 카테고리의 다른 글
LeetCode 1220. Count Vowels Permutation (0) | 2024.03.28 |
---|---|
LeetCode: Maximum Value of K Coins From Piles (0) | 2024.03.27 |
프로그래머스 LV3 표병합 (0) | 2024.03.18 |
프로그래머스 LV3 사라지는 발판 (0) | 2024.02.20 |
프로그래머스 LV3 부대복귀 (0) | 2024.02.16 |