작성
·
128
0
안녕하세요. 질문이 있습니다.
현재문제에서 풀이를 할 때
할인한 금액 + 배달료 를 합한것이 제일큰 아이들부터 전체 금액에서 빼주면서
전체 금액 - (할인한 금액 + 배달료 중 제일 큰 아이) > 예산금액 이라면
product의 요소를 하나씩 지워가는 형태로 로직을 짜고 마지막에 product의 length를 answer에 대입해서 풀었습니다.
이럴 때 생길 수 있는 문제가 있을까요 ? 로직은 아래와 같습니다.
function solution(m, product) {
let answer = 0;
// 전제 조건
// 상품비, 선물이 각각 10만원을 넘지 않는다.
// 상품비 하나를 50퍼센트 깍을 수 있는 할인권이 있는데 가장 큰 액수(배송료 + discount적용한 선물 가격)를 할인하면 될듯
// m이 예산
let sum = 1000000000;
let productArray = product;
let discountPricePlusDeliverFeeArray = [];
while (sum - m > 0) {
sum = 0;
discountPricePlusDeliverFeeArray = [];
for (let i = 0; i < productArray.length; i++) {
const giftPrice = productArray[i][0];
const deliverFee = productArray[i][1];
discountPricePlusDeliverFeeArray.push(giftPrice / 2 + deliverFee);
sum += giftPrice + deliverFee;
}
if (sum - m > 0) {
const mostExpensivePrice = Math.max(...discountPricePlusDeliverFeeArray);
const index = discountPricePlusDeliverFeeArray.findIndex(
(val) => val === mostExpensivePrice
);
const mostExpesiveGiftPrice = productArray[index][0];
const discountPrice = mostExpesiveGiftPrice / 2;
if (sum - discountPrice > m) {
productArray = productArray.filter((val, i) => i !== index);
sum = sum - mostExpensivePrice;
} else {
sum = sum - discountPrice;
}
}
}
answer = productArray.length;
return answer;