Description Link to heading

860.lemonade-change

Solution Link to heading

We can create an array to show the number of $5, $10, $20. When the number of $5 is less than 0, return false. When we receive $20, we should provide change with $10 preferentially.

Code Link to heading

#include <vector>
using std::vector;
class Solution {
  public:
    bool lemonadeChange(vector<int> &bills) {
        vector<int> num(3, 0);
        for (int i = 0; i < bills.size(); i++) {
            if (bills[i] == 5)
                num[0]++;
            else if (bills[i] == 10) {
                if (num[0] > 0) {
                    num[0]--;
                    num[1]++;
                } else {
                    return false;
                }
            } else {
                if (num[1] > 0) {
                    if (num[0] > 0) {
                        num[1]--;
                        num[0]--;
                    } else
                        return false;
                } else {
                    if (num[0] > 2)
                        num[0] -= 3;
                    else
                        return false;
                }
            }
        }
        return true;
    }
};