https://school.programmers.co.kr/learn/courses/30/lessons/77486?language=javascript
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
function solution(enroll, referral, seller, amount) {
const map = new Map();
enroll.forEach(name => {
map.set(name, {
name,
money: 0,
profits: [],
parent: null,
children: []
});
});
referral.forEach((parentName, idx) => {
if (parentName === "-") {
return;
}
const childName = enroll[idx];
const child = map.get(childName);
const parent = map.get(parentName);
parent.children.push(child);
child.parent = parent;
});
seller.forEach((seller, idx) => {
const profit = amount[idx] * 100;
map.get(seller).profits.push(profit);
});
for (let idx = enroll.length - 1; idx >= 0; idx--) {
const personName = enroll[idx];
const person = map.get(personName);
person.profits.forEach((profit, idx) => {
if (!profit) {
return;
}
const parentProfit = getParentProfit(profit);
person.money += profit - parentProfit;
person.parent?.profits.push(parentProfit);
});
}
return enroll.map(name => map.get(name).money);
}
const getParentProfit = (profit) => {
return parseInt(profit / 10);
}
'문제 풀이 > 프로그래머스' 카테고리의 다른 글
Lv.3 아이템 줍기 (0) | 2024.08.16 |
---|---|
Lv.4 징검다리 (0) | 2024.08.16 |
Lv.3 부대복귀 (0) | 2024.06.11 |
2018 KAKAO BLIND RECRUITMENT[1차] / 셔틀버스 (0) | 2024.06.11 |
Lv.3 연속 펄스 부분 수열의 합 (0) | 2024.06.11 |