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 62f9c29

Browse files
authored
feat: add js/ts solutions to lc problem: No.0860 (doocs#3423)
1 parent 9c8672e commit 62f9c29

File tree

6 files changed

+211
-26
lines changed

6 files changed

+211
-26
lines changed

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

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ func lemonadeChange(bills []int) bool {
196196

197197
```ts
198198
function lemonadeChange(bills: number[]): boolean {
199-
let five = 0;
200-
let ten = 0;
201-
for (let bill of bills) {
202-
switch (bill) {
199+
let [five, ten] = [0, 0];
200+
for (const x of bills) {
201+
switch (x) {
203202
case 5:
204203
five++;
205204
break;
@@ -208,11 +207,44 @@ function lemonadeChange(bills: number[]): boolean {
208207
ten++;
209208
break;
210209
case 20:
211-
if (ten !== 0) {
212-
ten -= 1;
213-
bill -= 10;
210+
if (ten) {
211+
ten--;
212+
five--;
213+
} else {
214+
five -= 3;
215+
}
216+
break;
217+
}
218+
219+
if (five < 0) {
220+
return false;
221+
}
222+
}
223+
return true;
224+
}
225+
```
226+
227+
#### JavaScript
228+
229+
```js
230+
function lemonadeChange(bills) {
231+
let [five, ten] = [0, 0];
232+
for (const x of bills) {
233+
switch (x) {
234+
case 5:
235+
five++;
236+
break;
237+
case 10:
238+
five--;
239+
ten++;
240+
break;
241+
case 20:
242+
if (ten) {
243+
ten--;
244+
five--;
245+
} else {
246+
five -= 3;
214247
}
215-
five -= bill / 5 - 1;
216248
break;
217249
}
218250

@@ -262,4 +294,42 @@ impl Solution {
262294

263295
<!-- solution:end -->
264296

297+
<!-- solution:start -->
298+
299+
### 方法二:一行
300+
301+
<!-- tabs:start -->
302+
303+
#### TypeScript
304+
305+
```ts
306+
const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
307+
bills.every(
308+
x => (
309+
(!(x ^ 5) && ++f) ||
310+
(!(x ^ 10) && (--f, ++t)) ||
311+
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
312+
f >= 0
313+
),
314+
);
315+
```
316+
317+
#### JavaScript
318+
319+
```js
320+
const lemonadeChange = (bills, f = 0, t = 0) =>
321+
bills.every(
322+
x => (
323+
(!(x ^ 5) && ++f) ||
324+
(!(x ^ 10) && (--f, ++t)) ||
325+
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
326+
f >= 0
327+
),
328+
);
329+
```
330+
331+
<!-- tabs:end -->
332+
333+
<!-- solution:end -->
334+
265335
<!-- problem:end -->

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

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929
<pre>
3030
<strong>Input:</strong> bills = [5,5,5,10,20]
3131
<strong>Output:</strong> true
32-
<strong>Explanation:</strong>
32+
<strong>Explanation:</strong>
3333
From the first 3 customers, we collect three 5ドル bills in order.
3434
From the fourth customer, we collect a 10ドル bill and give back a 5ドル.
3535
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.
4141
<pre>
4242
<strong>Input:</strong> bills = [5,5,10,10,20]
4343
<strong>Output:</strong> false
44-
<strong>Explanation:</strong>
44+
<strong>Explanation:</strong>
4545
From the first two customers in order, we collect two 5ドル bills.
4646
For the next two customers in order, we collect a 10ドル bill and give back a 5ドル bill.
4747
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 {
181181

182182
```ts
183183
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) {
188187
case 5:
189188
five++;
190189
break;
@@ -193,11 +192,44 @@ function lemonadeChange(bills: number[]): boolean {
193192
ten++;
194193
break;
195194
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;
199232
}
200-
five -= bill / 5 - 1;
201233
break;
202234
}
203235

@@ -247,4 +279,42 @@ impl Solution {
247279

248280
<!-- solution:end -->
249281

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+
250320
<!-- problem:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export function lemonadeChange(bills) {
2+
let [five, ten] = [0, 0];
3+
for (const x of bills) {
4+
switch (x) {
5+
case 5:
6+
five++;
7+
break;
8+
case 10:
9+
five--;
10+
ten++;
11+
break;
12+
case 20:
13+
if (ten) {
14+
ten--;
15+
five--;
16+
} else {
17+
five -= 3;
18+
}
19+
break;
20+
}
21+
22+
if (five < 0) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}

‎solution/0800-0899/0860.Lemonade Change/Solution.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
function lemonadeChange(bills: number[]): boolean {
2-
let five = 0;
3-
let ten = 0;
4-
for (let bill of bills) {
5-
switch (bill) {
2+
let [five, ten] = [0, 0];
3+
for (const x of bills) {
4+
switch (x) {
65
case 5:
76
five++;
87
break;
@@ -11,11 +10,12 @@ function lemonadeChange(bills: number[]): boolean {
1110
ten++;
1211
break;
1312
case 20:
14-
if (ten !== 0) {
15-
ten -= 1;
16-
bill -= 10;
13+
if (ten) {
14+
ten--;
15+
five--;
16+
} else {
17+
five -= 3;
1718
}
18-
five -= bill / 5 - 1;
1919
break;
2020
}
2121

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const lemonadeChange = (bills, f = 0, t = 0) =>
2+
bills.every(
3+
x => (
4+
(!(x ^ 5) && ++f) ||
5+
(!(x ^ 10) && (--f, ++t)) ||
6+
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
7+
f >= 0
8+
),
9+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
2+
bills.every(
3+
x => (
4+
(!(x ^ 5) && ++f) ||
5+
(!(x ^ 10) && (--f, ++t)) ||
6+
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
7+
f >= 0
8+
),
9+
);

0 commit comments

Comments
(0)

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