Skip to content

Commit 7d42e71

Browse files
committed
Add solution #860
1 parent f498c10 commit 7d42e71

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@
667667
857|[Minimum Cost to Hire K Workers](./0857-minimum-cost-to-hire-k-workers.js)|Hard|
668668
858|[Mirror Reflection](./0858-mirror-reflection.js)|Medium|
669669
859|[Buddy Strings](./0859-buddy-strings.js)|Easy|
670+
860|[Lemonade Change](./0860-lemonade-change.js)|Easy|
670671
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
671672
868|[Binary Gap](./0868-binary-gap.js)|Easy|
672673
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|

solutions/0860-lemonade-change.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 860. Lemonade Change
3+
* https://leetcode.com/problems/lemonade-change/
4+
* Difficulty: Easy
5+
*
6+
* At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you
7+
* and order one at a time (in the order specified by bills). Each customer will only buy one
8+
* lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to
9+
* each customer so that the net transaction is that the customer pays $5.
10+
*
11+
* Note that you do not have any change in hand at first.
12+
*
13+
* Given an integer array bills where bills[i] is the bill the ith customer pays, return true if
14+
* you can provide every customer with the correct change, or false otherwise.
15+
*/
16+
17+
/**
18+
* @param {number[]} bills
19+
* @return {boolean}
20+
*/
21+
var lemonadeChange = function(bills) {
22+
let fives = 0;
23+
let tens = 0;
24+
25+
for (const bill of bills) {
26+
if (bill === 5) {
27+
fives++;
28+
} else if (bill === 10) {
29+
if (fives === 0) return false;
30+
fives--;
31+
tens++;
32+
} else {
33+
if (tens > 0 && fives > 0) {
34+
tens--;
35+
fives--;
36+
} else if (fives >= 3) {
37+
fives -= 3;
38+
} else {
39+
return false;
40+
}
41+
}
42+
}
43+
44+
return true;
45+
};

0 commit comments

Comments
 (0)