Skip to content

Commit 5d0abee

Browse files
committed
860. 柠檬水找零
1 parent 45add12 commit 5d0abee

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
|704|[二分查找](https://leetcode.cn/problems/binary-search/)|[JavaScript](./algorithms/binary-search.js)|Easy|
187187
|707|[设计链表](https://leetcode.cn/problems/design-linked-list/)|[JavaScript](./algorithms/design-linked-list.js)|Medium|
188188
|844|[比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/)|[JavaScript](./algorithms/backspace-string-compare.js)|Easy|
189+
|860|[柠檬水找零](https://leetcode.cn/problems/lemonade-change/)|[JavaScript](./algorithms/lemonade-change.js)|Easy|
189190
|876|[链表的中间结点](https://leetcode.cn/problems/middle-of-the-linked-list/)|[JavaScript](./algorithms/middle-of-the-linked-list.js)|Easy|
190191
|897|[递增顺序搜索树](https://leetcode.cn/problems/increasing-order-search-tree/)|[JavaScript](./algorithms/increasing-order-search-tree.js)|Easy|
191192
|904|[水果成篮](https://leetcode.cn/problems/fruit-into-baskets/)|[JavaScript](./algorithms/fruit-into-baskets.js)|Medium|

algorithms/lemonade-change.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 860. 柠檬水找零
3+
* @param {number[]} bills
4+
* @return {boolean}
5+
*/
6+
var lemonadeChange = function (bills) {
7+
// 面额: 5 10 20
8+
9+
let five = 0;
10+
let ten = 0;
11+
12+
for (let i = 0; i < bills.length; i++) {
13+
const curr = bills[i];
14+
if (curr === 5) {
15+
five++;
16+
} else if (curr === 10) {
17+
ten++;
18+
five--; // 找零 5
19+
} else if (ten > 0) {
20+
// curr: 20, 找零 5, 10
21+
ten--;
22+
five--;
23+
} else {
24+
// curr: 20, 找零 5 * 3
25+
five -= 3;
26+
}
27+
if (five < 0) {
28+
return false;
29+
}
30+
}
31+
32+
return true;
33+
};

0 commit comments

Comments
 (0)