inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

4. 졸업선물

질문

141

열씨미살자!

작성한 질문수 87

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;

javascript 코테 준비 같이 해요!

답변 2

0

김태원

안녕하세요^^

다음 입력이 반례입니다.

5 27

8 6

2 2

4 3

4 5

6 4

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

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

0

김태원

안녕하세요^^

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

0

열씨미살자!

<!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>

0

열씨미살자!

요청하신 풀코드입니다!

continue를 사용하는 이유

0

75

2

정렬 가능 여부 판단하기

0

62

2

알고리즘 학습법 관련해서 질문드립니다.

0

80

1

코드 리뷰 부탁드립니다!

0

88

1

indexOf를 사용해서 풀어보았습니다 !!

0

67

1

저는 이런식으로 구현 해보았습니다 !!

0

62

1

12,13,14 강의 소리만 나오고 검은 화면입니다

0

97

3

반복문 최소화하고 indexOf 사용해서 풀어봤습니다

0

61

1

영상 보기 전에 직접 풀어봤습니다.

0

71

1

섹션1의 17번문제 이 풀이로 풀어도 될까요?

0

133

2

정규표현식으로 처리해도 상관없나요 ?

0

119

2

3칸씩 건너뛸 수 있을 경우

0

124

2

강의에 대해 질문있습니다.

0

133

2

Object와 Set을 이용해 풀어봤습니다.

0

116

2

이렇게 해도 되나요?

0

102

2

선생님 중복 단어나 중복관련 문제들은 set을 이용하면 좋을것 같습니다.

0

144

2

이렇게 풀어도 괜찮을까요?

0

136

1

이렇게 풀어도 괜찮을까요?

0

112

1

모든 아나그램 찾기에서 시간복잡도

0

98

1

코드리뷰 부탁드립니다.

0

129

1

for loop 탈출은 return 문으로 해도 되지 않나요?

0

129

1

투포인트알고리즘으로 풀어봤습니다.

0

138

0

코드 리뷰 부탁드립니다.

0

115

1

코드 맞게 작성한 거 아닌가여??

0

142

1