Skip to content

feat: add js/ts solutions to lc problem: No.0860 #3423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 78 additions & 8 deletions solution/0800-0899/0860.Lemonade Change/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,9 @@ func lemonadeChange(bills []int) bool {

```ts
function lemonadeChange(bills: number[]): boolean {
let five = 0;
let ten = 0;
for (let bill of bills) {
switch (bill) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
Expand All @@ -208,11 +207,44 @@ function lemonadeChange(bills: number[]): boolean {
ten++;
break;
case 20:
if (ten !== 0) {
ten -= 1;
bill -= 10;
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
break;
}

if (five < 0) {
return false;
}
}
return true;
}
```

#### JavaScript

```js
function lemonadeChange(bills) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
case 10:
five--;
ten++;
break;
case 20:
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
five -= bill / 5 - 1;
break;
}

Expand Down Expand Up @@ -262,4 +294,42 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:一行

<!-- tabs:start -->

#### TypeScript

```ts
const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
```

#### JavaScript

```js
const lemonadeChange = (bills, f = 0, t = 0) =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
90 changes: 80 additions & 10 deletions solution/0800-0899/0860.Lemonade Change/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:
<pre>
<strong>Input:</strong> bills = [5,5,5,10,20]
<strong>Output:</strong> true
<strong>Explanation:</strong>
<strong>Explanation:</strong>
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Expand All @@ -41,7 +41,7 @@ Since all customers got correct change, we output true.
<pre>
<strong>Input:</strong> bills = [5,5,10,10,20]
<strong>Output:</strong> false
<strong>Explanation:</strong>
<strong>Explanation:</strong>
From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
Expand Down Expand Up @@ -181,10 +181,9 @@ func lemonadeChange(bills []int) bool {

```ts
function lemonadeChange(bills: number[]): boolean {
let five = 0;
let ten = 0;
for (let bill of bills) {
switch (bill) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
Expand All @@ -193,11 +192,44 @@ function lemonadeChange(bills: number[]): boolean {
ten++;
break;
case 20:
if (ten !== 0) {
ten -= 1;
bill -= 10;
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
break;
}

if (five < 0) {
return false;
}
}
return true;
}
```

#### JavaScript

```js
function lemonadeChange(bills) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
case 10:
five--;
ten++;
break;
case 20:
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
five -= bill / 5 - 1;
break;
}

Expand Down Expand Up @@ -247,4 +279,42 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: One-liner

<!-- tabs:start -->

#### TypeScript

```ts
const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
```

#### JavaScript

```js
const lemonadeChange = (bills, f = 0, t = 0) =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
27 changes: 27 additions & 0 deletions solution/0800-0899/0860.Lemonade Change/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function lemonadeChange(bills) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
case 10:
five--;
ten++;
break;
case 20:
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
break;
}

if (five < 0) {
return false;
}
}
return true;
}
16 changes: 8 additions & 8 deletions solution/0800-0899/0860.Lemonade Change/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function lemonadeChange(bills: number[]): boolean {
let five = 0;
let ten = 0;
for (let bill of bills) {
switch (bill) {
let [five, ten] = [0, 0];
for (const x of bills) {
switch (x) {
case 5:
five++;
break;
Expand All @@ -11,11 +10,12 @@ function lemonadeChange(bills: number[]): boolean {
ten++;
break;
case 20:
if (ten !== 0) {
ten -= 1;
bill -= 10;
if (ten) {
ten--;
five--;
} else {
five -= 3;
}
five -= bill / 5 - 1;
break;
}

Expand Down
9 changes: 9 additions & 0 deletions solution/0800-0899/0860.Lemonade Change/Solution2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const lemonadeChange = (bills, f = 0, t = 0) =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
9 changes: 9 additions & 0 deletions solution/0800-0899/0860.Lemonade Change/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
bills.every(
x => (
(!(x ^ 5) && ++f) ||
(!(x ^ 10) && (--f, ++t)) ||
(!(x ^ 20) && (t ? (f--, t--) : (f -= 3), 1)),
f >= 0
),
);
Loading