https://school.programmers.co.kr/learn/courses/30/lessons/150368?language=javascript
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
function solution(users, emoticons) {
let answer = [0, 0];
function discountCombinations(discounts, numProducts) {
function generateCombo(currentCombo, depth) {
if (depth === numProducts) {
let plus = 0, sell = 0; //플러스 가입자수, 총 판매금액
//reduce를 사용해 이모티콘 총 구매금액을 계산
for (let user of users) {
let total = emoticons.reduce((sum, price, c) =>
sum + (currentCombo[c] >= user[0] ? price * (1 - currentCombo [c] * 0.01) : 0), 0
);
//총 구매한 금액이 목표금액보다 비싼경우 구매를 취소하고 플러스 가입
if (total >= user[1]) plus++;
else sell += total;
}
//그 경우의 수 마다 max값을 비교해서 최적의 결과를 answer에 넣어줌
if (plus > answer[0] || (plus === answer[0] && sell > answer[1])) {
answer = [plus, sell];
}
return;
}
//재귀
for (let discount of discounts) {
generateCombo([...currentCombo, discount], depth + 1);
}
}
generateCombo([], 0);
}
discountCombinations([10, 20, 30, 40], emoticons.length);
return answer;
}
재귀함수를 사용해서 각 이모티콘별로 할인되는 모든 경우의 수를 구하고, 그 경우의 수 중 max 값을 찾는
무식한 방법을 사용했다 ㅎㅎ ...
이모티콘의 갯수가 많지 않고 할인은 10 20 30 40 단 네가지의 경우의 수만 존재하기때문에 가능했던 풀이
'코딩테스트' 카테고리의 다른 글
[프로그래머스 / set ] 롤케이크 자르기 js javascript (0) | 2024.11.26 |
---|---|
[프로그래머스 | 스택 큐 ] 올바른 괄호 js javascript (0) | 2024.11.22 |
[프로그래머스 / 반복문] 피보나치 수 js javascript (0) | 2024.11.19 |
[프로그래머스 / 문자열] 이진 변환 반복하기 js javascript (1) | 2024.11.13 |
[PCCP 기출문제] 동영상 재생기 js javascript (0) | 2024.11.10 |