Lemonade Stand

ARRAY

Problem

At a lemonade stand, each lemonade costs $5. Customers can pay with $5, $10, or $20 bills.

Starting with no initial change, the goal is to determine if it is possible to provide the correct change to each customer based on the sequence of bills they use to pay.

The function should return true if correct change can be provided to all customers, otherwise, it should return false.

Examples

canProvideChange([5,5,10,10]) // true /* Why? From the first customer, we collect a $5 bill. For the second customer, we collect a $5 bill. For the third customer, we collect a $10 bill and give back a $5. For the forth customer, we collect a $10 bill and give back $5. Since all customers got correct change, we output true. */ canProvideChange([20,5,5,10,5]) // false /* Why? The first customer pays with a $20 bill. Since we cannot provide change, the answer is false. */
Loading...