29
29
<pre >
30
30
<strong >Input:</strong > bills = [5,5,5,10,20]
31
31
<strong >Output:</strong > true
32
- <strong >Explanation:</strong >
32
+ <strong >Explanation:</strong >
33
33
From the first 3 customers, we collect three $5 bills in order.
34
34
From the fourth customer, we collect a $10 bill and give back a $5.
35
35
From the fifth customer, we give a $10 bill and a $5 bill.
@@ -41,7 +41,7 @@ Since all customers got correct change, we output true.
41
41
<pre >
42
42
<strong >Input:</strong > bills = [5,5,10,10,20]
43
43
<strong >Output:</strong > false
44
- <strong >Explanation:</strong >
44
+ <strong >Explanation:</strong >
45
45
From the first two customers in order, we collect two $5 bills.
46
46
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
47
47
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
@@ -181,10 +181,9 @@ func lemonadeChange(bills []int) bool {
181
181
182
182
``` ts
183
183
function lemonadeChange(bills : number []): boolean {
184
- let five = 0 ;
185
- let ten = 0 ;
186
- for (let bill of bills ) {
187
- switch (bill ) {
184
+ let [five, ten] = [0 , 0 ];
185
+ for (const x of bills ) {
186
+ switch (x ) {
188
187
case 5 :
189
188
five ++ ;
190
189
break ;
@@ -193,11 +192,44 @@ function lemonadeChange(bills: number[]): boolean {
193
192
ten ++ ;
194
193
break ;
195
194
case 20 :
196
- if (ten !== 0 ) {
197
- ten -= 1 ;
198
- bill -= 10 ;
195
+ if (ten ) {
196
+ ten -- ;
197
+ five -- ;
198
+ } else {
199
+ five -= 3 ;
200
+ }
201
+ break ;
202
+ }
203
+
204
+ if (five < 0 ) {
205
+ return false ;
206
+ }
207
+ }
208
+ return true ;
209
+ }
210
+ ```
211
+
212
+ #### JavaScript
213
+
214
+ ``` js
215
+ function lemonadeChange (bills ) {
216
+ let [five, ten] = [0 , 0 ];
217
+ for (const x of bills) {
218
+ switch (x) {
219
+ case 5 :
220
+ five++ ;
221
+ break ;
222
+ case 10 :
223
+ five-- ;
224
+ ten++ ;
225
+ break ;
226
+ case 20 :
227
+ if (ten) {
228
+ ten-- ;
229
+ five-- ;
230
+ } else {
231
+ five -= 3 ;
199
232
}
200
- five -= bill / 5 - 1 ;
201
233
break ;
202
234
}
203
235
@@ -247,4 +279,42 @@ impl Solution {
247
279
248
280
<!-- solution: end -->
249
281
282
+ <!-- solution: start -->
283
+
284
+ ### Solution 2: One-liner
285
+
286
+ <!-- tabs: start -->
287
+
288
+ #### TypeScript
289
+
290
+ ``` ts
291
+ const lemonadeChange = (bills : number [], f = 0 , t = 0 ): boolean =>
292
+ bills .every (
293
+ x => (
294
+ (! (x ^ 5 ) && ++ f ) ||
295
+ (! (x ^ 10 ) && (-- f , ++ t )) ||
296
+ (! (x ^ 20 ) && (t ? (f -- , t -- ) : (f -= 3 ), 1 )),
297
+ f >= 0
298
+ ),
299
+ );
300
+ ```
301
+
302
+ #### JavaScript
303
+
304
+ ``` js
305
+ const lemonadeChange = (bills , f = 0 , t = 0 ) =>
306
+ bills .every (
307
+ x => (
308
+ (! (x ^ 5 ) && ++ f) ||
309
+ (! (x ^ 10 ) && (-- f, ++ t)) ||
310
+ (! (x ^ 20 ) && (t ? (f-- , t-- ) : (f -= 3 ), 1 )),
311
+ f >= 0
312
+ ),
313
+ );
314
+ ```
315
+
316
+ <!-- tabs: end -->
317
+
318
+ <!-- solution: end -->
319
+
250
320
<!-- problem: end -->
0 commit comments