-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path860-lemonade-change.js
More file actions
52 lines (48 loc) · 1.25 KB
/
860-lemonade-change.js
File metadata and controls
52 lines (48 loc) · 1.25 KB
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
/**
* @desc problem : Lemonade Change
* @desc site : leetcode
* @desc link : https://leetcode.com/problems/lemonade-change/
* @desc level: 1
*/
/**
* solution
* @param bills {Array<number>} 주문 배열
*/
const solution = (bills) => {
let answer = true;
const price = 5;
const amount = {
5: 0,
10: 0,
20: 0
};
for (let i = 0; i < bills.length; i++) {
const bill = bills[i];
amount[bill] = amount[bill] + 1;
const exchange = bill - price;
// console.log('받은 돈', bill, '잔액', amount, '거스름돈', exchange);
if (exchange === 5) {
if (amount['5'] > 0) {
amount['5'] = amount['5'] - 1;
} else {
answer = false;
break;
}
} else if (exchange === 15) {
if (amount['5'] && amount['10']) {
amount['5'] = amount['5'] - 1;
amount['10'] = amount['10'] - 1;
} else if (amount['5'] >= 3) {
amount['5'] = amount['5'] - 3;
} else {
answer = false;
break;
}
}
}
return answer;
};
console.log(solution([5, 5, 5, 10, 20]));
console.log(solution([5, 5, 10, 10, 20]));
console.log(solution([5, 5, 5, 5, 20, 20, 5, 5, 20, 5]));
console.log(solution([5, 5, 10, 20, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 5, 5, 20, 5, 20, 5]));