-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscount.js
91 lines (78 loc) · 1.99 KB
/
discount.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
/**
* @desc problem : 할인 행사
* @desc site : Programmers
* @desc link : https://school.programmers.co.kr/learn/courses/30/lessons/131127
* @desc level: 2
* @desc solution : 슬라이딩 윈도우
*/
/**
* solution
* @param {array} want : 구매 품목
* @param {array} number : 구매 갯수
* @param {array} discount : 할인 품목
*/
function solution(want, number, discount) {
const slice = 10;
let answer = 0;
let productMap = new Map();
//array to map
want.forEach((item, index) => {
productMap.set(item, number[index]);
});
for (let index = 0; index < slice; index++) {
let nextFlag = true;
const product = discount[index];
if (productMap.has(product)) {
productMap.set(product, productMap.get(product) - 1);
}
for (let count of productMap.values()) {
if (count > 0) {
nextFlag = false;
}
}
if (nextFlag) {
answer++;
}
}
for (let index = slice; index < discount.length; index++) {
let nextFlag = true;
const product = discount[index];
const prevProduct = discount[index - slice];
if (productMap.has(prevProduct)) {
productMap.set(prevProduct, productMap.get(prevProduct) + 1);
}
if (productMap.has(product)) {
productMap.set(product, productMap.get(product) - 1);
}
for (let count of productMap.values()) {
if (count > 0) {
nextFlag = false;
}
}
if (nextFlag) {
answer++;
}
}
return answer;
}
const answer = solution(
['banana', 'apple', 'rice', 'pork', 'pot'],
[3, 2, 2, 2, 1],
[
'banana',
'banana',
'banana',
'apple',
'apple',
'rice',
'rice',
'pork',
'pork',
'pot',
'pot',
'banana',
'apple',
'banana'
]
);
console.log(answer);