mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0860.柠檬水找零.md
This commit is contained in:
@ -164,24 +164,39 @@ class Solution {
|
||||
```python
|
||||
class Solution:
|
||||
def lemonadeChange(self, bills: List[int]) -> bool:
|
||||
five, ten = 0, 0
|
||||
five = 0
|
||||
ten = 0
|
||||
twenty = 0
|
||||
|
||||
for bill in bills:
|
||||
# 情况一:收到5美元
|
||||
if bill == 5:
|
||||
five += 1
|
||||
elif bill == 10:
|
||||
if five < 1: return False
|
||||
five -= 1
|
||||
|
||||
# 情况二:收到10美元
|
||||
if bill == 10:
|
||||
if five <= 0:
|
||||
return False
|
||||
ten += 1
|
||||
else:
|
||||
if ten > 0 and five > 0:
|
||||
ten -= 1
|
||||
five -= 1
|
||||
|
||||
# 情况三:收到20美元
|
||||
if bill == 20:
|
||||
# 先尝试使用10美元和5美元找零
|
||||
if five > 0 and ten > 0:
|
||||
five -= 1
|
||||
elif five > 2:
|
||||
ten -= 1
|
||||
#twenty += 1
|
||||
# 如果无法使用10美元找零,则尝试使用三张5美元找零
|
||||
elif five >= 3:
|
||||
five -= 3
|
||||
#twenty += 1
|
||||
else:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Go
|
||||
|
Reference in New Issue
Block a user