-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraduation-present.js
73 lines (66 loc) · 2.11 KB
/
graduation-present.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @desc problem : 졸업선물
* @desc site : Olympiad
* @desc level: 4
* @desc solution : 2차원배열 + 완전탐색
*/
/**
* solution
* @param {array} priceList : 선물 가격 리스트
* @param {number} budget : 총 예산
*/
function solution(priceList, budget) {
let answer = 0;
//학생 수
const studentLength = priceList.length;
//선물 구성 가격 길이
const priceLength = priceList[0].length;
//정답 로그
const log = [];
for (let order = 0; order < studentLength; order++) {
const orderList = [];
let totalCost = 0;
let orderCount = 1;
for (let i = 0; i < studentLength; i++) {
let price = 0;
let delivery = 0;
for (let j = 0; j < priceLength; j++) {
//j가 0일때 상품가격, 1일때 배송비
if(j < 1){
price = priceList[i][j];
//50% 쿠폰 적용가
if(order === i){
price = priceList[i][j] / 2;
}
}else{
delivery = priceList[i][j];
}
if(price&&delivery){
orderList.push(price + delivery);
}
}
}
//선물 완전탐색
for (let k = 0; k < studentLength; k++) {
totalCost = orderList[order];
orderCount = 1;
for (let l = k; l < orderList.length; l++) {
const item = orderList[l];
if(l !== order && totalCost + item <= budget){
orderCount++;
totalCost += item;
if(answer < orderCount){
log.push(`경우의수 : ${order} 회차, ${totalCost}, 구입횟수 : ${orderCount}`);
answer = orderCount;
}
}else{
break;
}
}
}
}
console.log(log[log.length - 1]);
return answer;
}
const answer = solution([[6,6], [2,2], [4,3], [4,5], [10,3]], 28);
console.log(answer);