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