diff --git a/solution/0800-0899/0860.Lemonade Change/README.md b/solution/0800-0899/0860.Lemonade Change/README.md index cb68c6d4b6270..76683943a9aeb 100644 --- a/solution/0800-0899/0860.Lemonade Change/README.md +++ b/solution/0800-0899/0860.Lemonade Change/README.md @@ -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; @@ -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; } @@ -262,4 +294,42 @@ impl Solution { + + +### 方法二:一行 + + + +#### 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 + ), + ); +``` + + + + + diff --git a/solution/0800-0899/0860.Lemonade Change/README_EN.md b/solution/0800-0899/0860.Lemonade Change/README_EN.md index 17803978c4305..be10d6e45ba07 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,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;
         }
 
@@ -247,4 +279,42 @@ 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
+        ),
+    );
+```
+
+#### 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/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;
+}
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;
         }
 
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
+        ),
+    );
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
+        ),
+    );