인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

열씨미살자!님의 프로필 이미지
열씨미살자!

작성한 질문수

자바스크립트 알고리즘 문제풀이 입문(코딩테스트 대비)

4. 졸업선물

질문

작성

·

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;

답변 2

0

김태원님의 프로필 이미지
김태원
지식공유자

안녕하세요^^

다음 입력이 반례입니다.

5 27

8 6

2 2

4 3

4 5

6 4

답은 4입니다. (6, 4)를 할인받으면 4명까지 선물할 수 있습니다. 

위에 코드로는 3이 나오는 것 같습니다. 

0

김태원님의 프로필 이미지
김태원
지식공유자

안녕하세요^^

제가 실행시켜서 테스트 해 볼 수 있게  강의영상과 같은 html 풀코드를 주세요.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Document</title>

</head>

<body>

 <script>

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;

}



let arr = [

[6, 6],

[2, 2],

[4, 3],

[4, 5],

[10, 3],

];

console.log(solution(28, arr));

</script>

</body>

</html>

요청하신 풀코드입니다!

열씨미살자!님의 프로필 이미지
열씨미살자!

작성한 질문수

질문하기