From d7fdf3d0128511ea3e5d3600288a3bc01c124ad4 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 15 Aug 2024 17:41:40 +0300 Subject: [PATCH 1/6] feat: update ts solution to lc problem: No.0860 --- .../0800-0899/0860.Lemonade Change/README.md | 44 +++++++++---------- .../0860.Lemonade Change/README_EN.md | 20 ++++----- .../0860.Lemonade Change/Solution.ts | 16 +++---- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md index cb68c6d4b6270..2f38ba162ac70 100644 --- a/solution/0800-0899/0860.Lemonade Change/README.md +++ b/solution/0800-0899/0860.Lemonade Change/README.md @@ -196,31 +196,31 @@ func lemonadeChange(bills []int) bool { ```ts function lemonadeChange(bills: number[]): boolean { - let five = 0; - let ten = 0; - for (let bill of bills) { - switch (bill) { - case 5: - five++; - break; - case 10: - five--; - ten++; - break; - case 20: - if (ten !== 0) { - ten -= 1; - bill -= 10; - } - five -= bill / 5 - 1; - break; + 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; - } + if (five < 0) { + return false } - return true; + } + return true } ``` diff --git a/solution/0800-0899/0860.Lemonade Change/README_EN.md b/solution/0800-0899/0860.Lemonade Change/README_EN.md index 17803978c4305..29598642a18b7 100644 --- a/solution/0800-0899/0860.Lemonade Change/README_EN.md +++ b/solution/0800-0899/0860.Lemonade Change/README_EN.md @@ -29,7 +29,7 @@ tags:
 Input: bills = [5,5,5,10,20]
 Output: true
-Explanation: 
+Explanation:
 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.
@@ -41,7 +41,7 @@ Since all customers got correct change, we output true.
 
 Input: bills = [5,5,10,10,20]
 Output: false
-Explanation: 
+Explanation:
 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.
@@ -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;
@@ -193,11 +192,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;
         }
 
diff --git a/solution/0800-0899/0860.Lemonade Change/Solution.ts b/solution/0800-0899/0860.Lemonade Change/Solution.ts
index f5891b9105c19..f3a9550a8a9a3 100644
--- a/solution/0800-0899/0860.Lemonade Change/Solution.ts	
+++ b/solution/0800-0899/0860.Lemonade Change/Solution.ts	
@@ -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;
@@ -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;
         }
 

From e9525933e82c731ec726f0d97f6817ca75e7daec Mon Sep 17 00:00:00 2001
From: rain84 
Date: Thu, 15 Aug 2024 17:42:21 +0300
Subject: [PATCH 2/6] feat: add js solution to lc problem: No.0860

---
 .../0800-0899/0860.Lemonade Change/README.md  | 32 +++++++++++++++++++
 .../0860.Lemonade Change/README_EN.md         | 32 +++++++++++++++++++
 .../0860.Lemonade Change/Solution.js          | 27 ++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 solution/0800-0899/0860.Lemonade Change/Solution.js

diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md
index 2f38ba162ac70..f181b045d3644 100644
--- a/solution/0800-0899/0860.Lemonade Change/README.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README.md	
@@ -224,6 +224,38 @@ function lemonadeChange(bills: number[]): boolean {
 }
 ```
 
+#### 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;
+                }
+                break;
+        }
+
+        if (five < 0) {
+            return false;
+        }
+    }
+    return true;
+}
+```
+
 #### Rust
 
 ```rust
diff --git a/solution/0800-0899/0860.Lemonade Change/README_EN.md b/solution/0800-0899/0860.Lemonade Change/README_EN.md
index 29598642a18b7..aad5410c2ff39 100644
--- a/solution/0800-0899/0860.Lemonade Change/README_EN.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README_EN.md	
@@ -209,6 +209,38 @@ function lemonadeChange(bills: number[]): boolean {
 }
 ```
 
+#### 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;
+                }
+                break;
+        }
+
+        if (five < 0) {
+            return false;
+        }
+    }
+    return true;
+}
+```
+
 #### Rust
 
 ```rust
diff --git a/solution/0800-0899/0860.Lemonade Change/Solution.js b/solution/0800-0899/0860.Lemonade Change/Solution.js
new file mode 100644
index 0000000000000..862baca9ee584
--- /dev/null
+++ b/solution/0800-0899/0860.Lemonade Change/Solution.js	
@@ -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;
+}

From 5258841d006510a44cf1cfa47da66cc50c079bce Mon Sep 17 00:00:00 2001
From: rain84 
Date: Thu, 15 Aug 2024 18:16:37 +0300
Subject: [PATCH 3/6] feat: add ts solution to lc problem: No.0860

---
 .../0800-0899/0860.Lemonade Change/README.md  | 24 +++++++++++++++++++
 .../0860.Lemonade Change/README_EN.md         | 24 +++++++++++++++++++
 .../0860.Lemonade Change/Solution2.ts         |  9 +++++++
 3 files changed, 57 insertions(+)
 create mode 100644 solution/0800-0899/0860.Lemonade Change/Solution2.ts

diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md
index f181b045d3644..aa0b52d0c175a 100644
--- a/solution/0800-0899/0860.Lemonade Change/README.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README.md	
@@ -294,4 +294,28 @@ impl Solution {
 
 
 
+
+
+### Solution 2: One-liner
+
+
+
+#### 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
+        ),
+    );
+```
+
+
+
+
+
 
diff --git a/solution/0800-0899/0860.Lemonade Change/README_EN.md b/solution/0800-0899/0860.Lemonade Change/README_EN.md
index aad5410c2ff39..9a4028fc6671d 100644
--- a/solution/0800-0899/0860.Lemonade Change/README_EN.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README_EN.md	
@@ -279,4 +279,28 @@ impl Solution {
 
 
 
+
+
+### Solution 2: One-liner
+
+
+
+#### 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
+        ),
+    );
+```
+
+
+
+
+
 
diff --git a/solution/0800-0899/0860.Lemonade Change/Solution2.ts b/solution/0800-0899/0860.Lemonade Change/Solution2.ts
new file mode 100644
index 0000000000000..3537c47c652e3
--- /dev/null
+++ b/solution/0800-0899/0860.Lemonade Change/Solution2.ts	
@@ -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
+        ),
+    );

From 19e8f5465e93b54e09357b6bf3a3e4e8d7a3795f Mon Sep 17 00:00:00 2001
From: rain84 
Date: Thu, 15 Aug 2024 18:17:53 +0300
Subject: [PATCH 4/6] feat: add js solution to lc problem: No.0860

---
 solution/0800-0899/0860.Lemonade Change/README.md  | 14 ++++++++++++++
 .../0800-0899/0860.Lemonade Change/README_EN.md    | 14 ++++++++++++++
 .../0800-0899/0860.Lemonade Change/Solution2.js    |  9 +++++++++
 3 files changed, 37 insertions(+)
 create mode 100644 solution/0800-0899/0860.Lemonade Change/Solution2.js

diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md
index aa0b52d0c175a..a7d10da1ae5f8 100644
--- a/solution/0800-0899/0860.Lemonade Change/README.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README.md	
@@ -314,6 +314,20 @@ const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
     );
 ```
 
+#### 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
+        ),
+    );
+```
+
 
 
 
diff --git a/solution/0800-0899/0860.Lemonade Change/README_EN.md b/solution/0800-0899/0860.Lemonade Change/README_EN.md
index 9a4028fc6671d..be10d6e45ba07 100644
--- a/solution/0800-0899/0860.Lemonade Change/README_EN.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README_EN.md	
@@ -299,6 +299,20 @@ const lemonadeChange = (bills: number[], f = 0, t = 0): boolean =>
     );
 ```
 
+#### 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
+        ),
+    );
+```
+
 
 
 
diff --git a/solution/0800-0899/0860.Lemonade Change/Solution2.js b/solution/0800-0899/0860.Lemonade Change/Solution2.js
new file mode 100644
index 0000000000000..6cc5fd8140e7b
--- /dev/null
+++ b/solution/0800-0899/0860.Lemonade Change/Solution2.js	
@@ -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
+        ),
+    );

From 252ab1c67ca473cda0331ee303e8985f561b691e Mon Sep 17 00:00:00 2001
From: rain84 
Date: Thu, 15 Aug 2024 15:19:20 +0000
Subject: [PATCH 5/6] style: format code and docs with prettier

---
 .../0800-0899/0860.Lemonade Change/README.md  | 44 +++++++++----------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md
index a7d10da1ae5f8..3d361266e809b 100644
--- a/solution/0800-0899/0860.Lemonade Change/README.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README.md	
@@ -196,31 +196,31 @@ func lemonadeChange(bills []int) bool {
 
 ```ts
 function lemonadeChange(bills: number[]): boolean {
-  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
+    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;
         }
-        break
-    }
 
-    if (five < 0) {
-      return false
+        if (five < 0) {
+            return false;
+        }
     }
-  }
-  return true
+    return true;
 }
 ```
 

From e8ad01bde4e5c9332e336682535dbc6bf40f0458 Mon Sep 17 00:00:00 2001
From: Libin YANG 
Date: Fri, 16 Aug 2024 08:21:43 +0800
Subject: [PATCH 6/6] Update README.md

---
 solution/0800-0899/0860.Lemonade Change/README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md
index 3d361266e809b..76683943a9aeb 100644
--- a/solution/0800-0899/0860.Lemonade Change/README.md	
+++ b/solution/0800-0899/0860.Lemonade Change/README.md	
@@ -296,7 +296,7 @@ impl Solution {
 
 
 
-### Solution 2: One-liner
+### 方法二:一行