-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkakao-emoticon-event.js
96 lines (84 loc) · 2.07 KB
/
kakao-emoticon-event.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @desc problem : 이모티콘 할인 행사
* @desc site : 프로그래머스
* @desc link : https://school.programmers.co.kr/learn/courses/30/lessons/150368
* @desc level: 2
*/
const getDiscountPrice = (discount, price) => {
return price - price * (discount / 100);
};
/**
*
* @param users {array<[number, number]>}
* @param emoticons {array<number>}
*/
const solution = (users, emoticons) => {
const discountPercentages = [10, 20, 30, 40];
const permutations = [];
const tempPermutations = [];
const result = {
maxSales: 0,
maxSubscribe: 0
};
const dfs = (depth, count) => {
if (depth === count) {
permutations.push([...tempPermutations]);
return;
}
for (let i = 0; i < discountPercentages.length; i++) {
tempPermutations[depth] = discountPercentages[i];
dfs(depth + 1, emoticons.length);
}
};
dfs(0, emoticons.length);
permutations.forEach((emoticonDiscount, index) => {
let totalSubscribe = 0;
let totalSales = 0;
users.forEach((user, userIndex) => {
const userMaxDiscount = user[0];
const userMaxAmount = user[1];
let totalPurchase = 0;
emoticons.forEach((price, index) => {
if (emoticonDiscount[index] >= userMaxDiscount) {
totalPurchase += getDiscountPrice(emoticonDiscount[index], price);
}
});
if (totalPurchase >= userMaxAmount) {
totalSubscribe += 1;
} else {
totalSales += totalPurchase;
}
});
if (totalSubscribe > result.maxSubscribe) {
result.maxSubscribe = totalSubscribe;
result.maxSales = totalSales;
}
if (totalSubscribe === result.maxSubscribe) {
result.maxSales = Math.max(result.maxSales, totalSales);
}
});
return [result.maxSubscribe, result.maxSales];
};
console.log(
solution(
[
[40, 10000],
[25, 10000]
],
[7000, 9000]
)
);
console.log(
solution(
[
[40, 2900],
[23, 10000],
[11, 5200],
[5, 5900],
[40, 3100],
[27, 9200],
[32, 6900]
],
[1300, 1500, 1600, 4900]
)
);