Leet - 860

Having fun on Leetcode with C++, Java, Python & GO

Problem 860. Lemonade Change

My attempt(s)

Code

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int bills5=0;
        int bills10=0;
        for (int i=0; i<bills.size(); i++){
            if (bills[i]==5)
                bills5++;
            else if (bills[i]==10){
                if (bills5<0) 
                    return false;
                else 
                    bills5--;
                bills10++;
            }
                
            else{
                if ((bills10>0) && (bills5>=1)){
                    bills10--;
                    bills5--;
                }
                else if (bills5>=3)
                    bills5=bills5-3;
                else
                    return false;
            }
        }
        return true;
    }
};

Explanation of idea

Good solution ref.

Code

Analysis

Licensed under CC BY-NC-SA 4.0