860. Lemonade Change
2021. 10. 24. 22:54ㆍLeetcode
Description:
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.
Note that you don't have any change in hand at first.
Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with correct change, or false otherwise.
'''
[1] Greedy)
idea::
1. keep the number of five dollars and ten dollars.
code::
(below)
-T/C: O(n) # n==len(bills)
-S/C: O(1)
'''
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five=0
ten=0
# twenty=0
for bill in bills:
if bill==5:
five+=1
elif bill==10:
if five<1:
return False
ten+=1
five-=1
else:
if five>=1 and ten>=1:
ten-=1
five-=1
elif five>=3:
five-=3
else:
return False
twenty+=1
return True
'Leetcode' 카테고리의 다른 글
199. Binary Tree Right Side View (0) | 2021.10.25 |
---|---|
103. Binary Tree Zigzag Level Order Traversal (0) | 2021.10.25 |
889. Construct Binary Tree from Preorder and Postorder Traversal (0) | 2021.10.23 |
654. Maximum Binary Tree (0) | 2021.10.23 |
187. Repeated DNA Sequences (0) | 2021.10.23 |