Skip to content

Commit ca2e67f

Browse files
committed
[LeetCode Sync] Runtime - 3 ms (75.28%), Memory - 21.9 MB (77.76%)
1 parent d0979bf commit ca2e67f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p>At a lemonade stand, each lemonade costs <code>$5</code>. 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 <code>$5</code>, <code>$10</code>, or <code>$20</code> bill. You must provide the correct change to each customer so that the net transaction is that the customer pays <code>$5</code>.</p>
2+
3+
<p>Note that you do not have any change in hand at first.</p>
4+
5+
<p>Given an integer array <code>bills</code> where <code>bills[i]</code> is the bill the <code>i<sup>th</sup></code> customer pays, return <code>true</code> <em>if you can provide every customer with the correct change, or</em> <code>false</code> <em>otherwise</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> bills = [5,5,5,10,20]
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong>
14+
From the first 3 customers, we collect three $5 bills in order.
15+
From the fourth customer, we collect a $10 bill and give back a $5.
16+
From the fifth customer, we give a $10 bill and a $5 bill.
17+
Since all customers got correct change, we output true.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> bills = [5,5,10,10,20]
24+
<strong>Output:</strong> false
25+
<strong>Explanation:</strong>
26+
From the first two customers in order, we collect two $5 bills.
27+
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
28+
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
29+
Since not every customer received the correct change, the answer is false.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= bills.length &lt;= 10<sup>5</sup></code></li>
37+
<li><code>bills[i]</code> is either <code>5</code>, <code>10</code>, or <code>20</code>.</li>
38+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def lemonadeChange(self, bills: List[int]) -> bool:
3+
fives = tens = 0
4+
for bill in bills:
5+
if bill == 5:
6+
fives += 1
7+
elif bill == 10:
8+
tens += 1
9+
fives -= 1
10+
else: #20
11+
if tens:
12+
tens -= 1
13+
fives -= 1
14+
else:
15+
fives -= 3
16+
if fives < 0:
17+
return False
18+
return True

0 commit comments

Comments
 (0)