← Home

860. Lemonade Change

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
860

The trick here is to name the state correctly, then let the implementation follow. For 860. Lemonade Change, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are lemonadeChange.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02At a lemonade stand, each lemonade costs $5. 
03
04Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).
05
06Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill.  You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.
07
08Note that you don't have any change in hand at first.
09
10Return true if and only if you can provide every customer with correct change.
11
12 
13
14Example 1:
15
16Input: [5,5,5,10,20]
17Output: true
18Explanation: 
19From the first 3 customers, we collect three $5 bills in order.
20From the fourth customer, we collect a $10 bill and give back a $5.
21From the fifth customer, we give a $10 bill and a $5 bill.
22Since all customers got correct change, we output true.
23Example 2:
24
25Input: [5,5,10]
26Output: true
27Example 3:
28
29Input: [10,10]
30Output: false
31Example 4:
32
33Input: [5,5,10,10,20]
34Output: false
35Explanation: 
36From the first two customers in order, we collect two $5 bills.
37For the next two customers in order, we collect a $10 bill and give back a $5 bill.
38For the last customer, we can't give change of $15 back because we only have two $10 bills.
39Since not every customer received correct change, the answer is false.
40 
41
42Note:
43
440 <= bills.length <= 10000
45bills[i] will be either 5, 10, or 20.
46**/
47
48//Runtime: 16 ms, faster than 97.60% of C++ online submissions for Lemonade Change.
49//Memory Usage: 9.7 MB, less than 100.00% of C++ online submissions for Lemonade Change.
50class Solution {
51public:
52    bool lemonadeChange(vector<int>& bills) {
53        vector<int> coins = {0, 0, 0};
54        
55        for(int bill : bills){
56            if(bill == 5){
57                coins[0] += 1;
58            }else if(bill == 10){
59                if(coins[0] >= 1){
60                    coins[0] -= 1;
61                    coins[1] += 1;
62                }else{
63                    return false;
64                }
65            }else if(bill == 20){
66                if(coins[1] >= 1 && coins[0] >= 1){
67                    coins[1] -= 1;
68                    coins[0] -= 1;
69                    coins[2] += 1;
70                }else if(coins[0] >= 3){
71                    coins[0] -= 3;
72                }else{
73                    return false;
74                }
75            }
76        }
77        
78        return true;
79    }
80};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.