Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb91531

Browse files
🐱(greedy): 860. 柠檬水找零
1 parent 5d78017 commit cb91531

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎docs/algorithm/greedy/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,49 @@ class Solution {
664664
- 时间复杂度:`O(n)`
665665
- 空间复杂度:`O(n)`
666666

667+
## 860. 柠檬水找零
668+
669+
[原题链接](https://leetcode-cn.com/problems/lemonade-change/)
670+
671+
### 思路
672+
673+
按顺序模拟找零顺序即可。
674+
675+
```python
676+
class Solution:
677+
def lemonadeChange(self, bills: List[int]) -> bool:
678+
count_5 = 0
679+
count_10 = 0
680+
681+
for bill in bills:
682+
if bill == 5:
683+
count_5 += 1
684+
elif bill == 10:
685+
if count_5 == 0:
686+
return False
687+
count_5 -= 1
688+
count_10 += 1
689+
else:
690+
if count_10 == 0:
691+
if count_5 < 3:
692+
return False
693+
else:
694+
count_5 -= 3
695+
else:
696+
if count_5 == 0:
697+
return False
698+
else:
699+
count_10 -= 1
700+
count_5 -= 1
701+
702+
return True
703+
```
704+
705+
### 复杂度
706+
707+
- 时间复杂度:O(n)
708+
- 空间复杂度:O(1)
709+
667710
## 955. 删列造序 II
668711

669712
[原题链接](https://leetcode-cn.com/problems/delete-columns-to-make-sorted-ii/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /