-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path860.柠檬水找零.js
More file actions
46 lines (45 loc) · 1011 Bytes
/
860.柠檬水找零.js
File metadata and controls
46 lines (45 loc) · 1011 Bytes
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
/*
* @lc app=leetcode.cn id=860 lang=javascript
*
* [860] 柠檬水找零
*/
// @lc code=start
/**
* @param {number[]} bills
* @return {boolean}
*/
var lemonadeChange = function (bills) {
let five = 0,
ten = 0;
for (let bill of bills) {
// 入账 5 元
if (bill === 5) {
five++;
}
// 入账 10 元
else if (bill === 10) {
if (five) {
// 帐内有 5 元(可找零)
five--;
ten++;
} else {
return false;
}
}
// 入账 20 元
else {
if (ten && five) {
// 帐内有 10 元和 5 元(可找零)
ten--;
five--;
} else if (five > 2) {
// 帐内有2张以上 5 元(可找零)
five -= 3;
} else {
return false;
}
}
}
return true;
};
// @lc code=end