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 8b3d0d1

Browse files
feat: add solutions to lc problem: No.0860 (doocs#609)
No.0860.Lemonade Change
1 parent 3a9a02a commit 8b3d0d1

File tree

3 files changed

+104
-38
lines changed

3 files changed

+104
-38
lines changed

‎solution/0800-0899/0860.Lemonade Change/README.md‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,56 @@
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```python
74-
74+
class Solution:
75+
def lemonadeChange(self, bills: List[int]) -> bool:
76+
five, ten = 0, 0
77+
for bill in bills:
78+
if bill == 5:
79+
five += 1
80+
elif bill == 10:
81+
ten += 1
82+
five -= 1
83+
elif bill == 20:
84+
if ten > 0:
85+
ten -= 1
86+
five -= 1
87+
else:
88+
five -= 3
89+
if five < 0:
90+
return False
91+
return True
7592
```
7693

7794
### **Java**
7895

7996
<!-- 这里可写当前语言的特殊实现逻辑 -->
8097

8198
```java
82-
99+
class Solution {
100+
public boolean lemonadeChange(int[] bills) {
101+
int fives = 0, tens = 0;
102+
for (int bill : bills) {
103+
if (bill == 5) {
104+
++fives;
105+
} else if (bill == 10) {
106+
++tens;
107+
if (--fives < 0) {
108+
return false;
109+
}
110+
} else {
111+
if (tens >= 1 && fives >= 1) {
112+
--tens;
113+
--fives;
114+
} else if (fives >= 3) {
115+
fives -= 3;
116+
} else {
117+
return false;
118+
}
119+
}
120+
}
121+
return true;
122+
}
123+
}
83124
```
84125

85126
### **...**

‎solution/0800-0899/0860.Lemonade Change/README_EN.md‎

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,20 @@
66

77
<p>At a lemonade stand, each lemonade costs <code>5ドル</code>.&nbsp;</p>
88

9-
10-
119
<p>Customers are standing in a queue to buy from you, and order one at a time (in the order specified by <code>bills</code>).</p>
1210

13-
14-
1511
<p>Each customer will only buy one lemonade and&nbsp;pay with either a <code>5ドル</code>, <code>10ドル</code>, or <code>20ドル</code> bill.&nbsp; You must provide the correct change to each customer, so that the net transaction is that the customer pays 5ドル.</p>
1612

17-
18-
1913
<p>Note that you don&#39;t have any change&nbsp;in hand at first.</p>
2014

21-
22-
2315
<p>Return <code>true</code>&nbsp;if and only if you can provide every customer with correct change.</p>
2416

25-
26-
2717
<p>&nbsp;</p>
2818

29-
30-
3119
<div>
3220

3321
<p><strong>Example 1:</strong></p>
3422

35-
36-
3723
<pre>
3824

3925
<strong>Input: </strong><span id="example-input-1-1">[5,5,5,10,20]</span>
@@ -52,14 +38,10 @@ Since all customers got correct change, we output true.
5238

5339
</pre>
5440

55-
56-
5741
<div>
5842

5943
<p><strong>Example 2:</strong></p>
6044

61-
62-
6345
<pre>
6446

6547
<strong>Input: </strong><span id="example-input-2-1">[5,5,10]</span>
@@ -68,14 +50,10 @@ Since all customers got correct change, we output true.
6850

6951
</pre>
7052

71-
72-
7353
<div>
7454

7555
<p><strong>Example 3:</strong></p>
7656

77-
78-
7957
<pre>
8058

8159
<strong>Input: </strong><span id="example-input-3-1">[10,10]</span>
@@ -84,14 +62,10 @@ Since all customers got correct change, we output true.
8462

8563
</pre>
8664

87-
88-
8965
<div>
9066

9167
<p><strong>Example 4:</strong></p>
9268

93-
94-
9569
<pre>
9670

9771
<strong>Input: </strong><span id="example-input-4-1">[5,5,10,10,20]</span>
@@ -110,16 +84,10 @@ Since not every customer received correct change, the answer is false.
11084

11185
</pre>
11286

113-
114-
11587
<p>&nbsp;</p>
11688

117-
118-
11989
<p><strong><span>Note:</span></strong></p>
12090

121-
122-
12391
<ul>
12492
<li><code>0 &lt;= bills.length &lt;= 10000</code></li>
12593
<li><code>bills[i]</code>&nbsp;will be either&nbsp;<code>5</code>, <code>10</code>, or <code>20</code>.</li>
@@ -133,22 +101,61 @@ Since not every customer received correct change, the answer is false.
133101

134102
</div>
135103

136-
137-
138104
## Solutions
139105

140106
<!-- tabs:start -->
141107

142108
### **Python3**
143109

144110
```python
145-
111+
class Solution:
112+
def lemonadeChange(self, bills: List[int]) -> bool:
113+
five, ten = 0, 0
114+
for bill in bills:
115+
if bill == 5:
116+
five += 1
117+
elif bill == 10:
118+
ten += 1
119+
five -= 1
120+
elif bill == 20:
121+
if ten > 0:
122+
ten -= 1
123+
five -= 1
124+
else:
125+
five -= 3
126+
if five < 0:
127+
return False
128+
return True
146129
```
147130

148131
### **Java**
149132

150133
```java
151-
134+
class Solution {
135+
public boolean lemonadeChange(int[] bills) {
136+
int fives = 0, tens = 0;
137+
for (int bill : bills) {
138+
if (bill == 5) {
139+
++fives;
140+
} else if (bill == 10) {
141+
++tens;
142+
if (--fives < 0) {
143+
return false;
144+
}
145+
} else {
146+
if (tens >= 1 && fives >= 1) {
147+
--tens;
148+
--fives;
149+
} else if (fives >= 3) {
150+
fives -= 3;
151+
} else {
152+
return false;
153+
}
154+
}
155+
}
156+
return true;
157+
}
158+
}
152159
```
153160

154161
### **...**
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+
five, ten = 0, 0
4+
for bill in bills:
5+
if bill == 5:
6+
five += 1
7+
elif bill == 10:
8+
ten += 1
9+
five -= 1
10+
elif bill == 20:
11+
if ten > 0:
12+
ten -= 1
13+
five -= 1
14+
else:
15+
five -= 3
16+
if five < 0:
17+
return False
18+
return True

0 commit comments

Comments
(0)

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