💢☠💫Easiest 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition





ApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/433e9ac0-2515-4d05-9b1f-e1d6121b7a3d_1723718859.2981706.jpeg)
![Screenshot 2024-08-15 161558.png](https://assets.leetcode.com/users/images/cdb49860-0e61-4eee-93da-0e5587a3398e_1723718871.8910623.png)
![Screenshot 2024-08-15 161156.png](https://assets.leetcode.com/users/images/5535c526-5220-40e6-a999-27bb7d59c543_1723718880.9173186.png)
![Screenshot 2024-08-15 161007.png](https://assets.leetcode.com/users/images/0105e27b-81e4-45a9-a3d9-56007930386d_1723718896.3018558.png)
![Screenshot 2024-08-15 160750.png](https://assets.leetcode.com/users/images/d71fdb50-70c6-4cbe-8990-8e58780f199c_1723718906.7879345.png)
![Screenshot 2024-08-15 160521.png](https://assets.leetcode.com/users/images/f106f0d6-4c50-4c81-a733-2ee27ee9b5ba_1723718916.5007813.png)
![Screenshot 2024-08-15 155601.png](https://assets.leetcode.com/users/images/cd3e2da2-62f5-4d85-acf1-c57c7636f9f3_1723718931.5713193.png)


![Screenshot 2024-12-31 235517.png](https://assets.leetcode.com/users/images/9c2c5e96-5241-4e93-95e0-8798affc173f_1735669539.6983507.png)

```javascript []
//JavaScript Code
/**
 * @param {number[]} bills
 * @return {boolean}
 */
var lemonadeChange = function(bills) {
    let five = 0, ten = 0;
        for(let bill of bills) {
            if( bill == 5)
                five++;
            
            else if( bill == 10) {
                if( five >= 1) {
                    five--;
                    ten++;
                }
                else
                    return false;
            }
            else {
                if(five >= 1 && ten >= 1) {
                    ten--;
                    five--;
                }
                else if( five >= 3)
                    five -= 3;
                else
                    return false;
            }
        }
        return true;
};
```
```python3 []
#Python3 Code
class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        cur_five, cur_ten = 0, 0
        for doller in bills:
            if doller == 5:
                cur_five += 1
            elif doller == 10:
                cur_five -= 1
                cur_ten += 1
            elif cur_ten > 0:
                cur_ten -= 1
                cur_five -= 1
            else:
                cur_five -= 3
            
            if cur_five < 0:
                return False
        
        return True
```
```C++ []
//C++ Code
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define FOR(a, b, c) for (int a = b; a < c; a++)
#define FOR1(a, b, c) for (int a = b; a <= c; ++a)
#define Rep(i, n) FOR(i, 0, n)
#define Rep1(i, n) FOR1(i, 1, n)
#define RepA(ele, nums) for (auto& ele : nums)
#define WHL(i, n) while (i < n)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0, ten = 0;
        RepA(bill, bills) {
            if( bill == 5)
                five++;
            
            else if( bill == 10) {
                if( five >= 1) {
                    five--;
                    ten++;
                }
                else
                    return false;
            }
            else {
                if(five >= 1 && ten >= 1) {
                    ten--;
                    five--;
                }
                else if( five >= 3)
                    five -= 3;
                else
                    return false;
            }
        }
        return true;
    }
};
```
```Java []
//Java Code
class Solution {
    public boolean lemonadeChange(int[] bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            if( bill == 5)
                five++;
            
            else if( bill == 10) {
                if( five >= 1) {
                    five--;
                    ten++;
                }
                else
                    return false;
            }
            else {
                if(five >= 1 && ten >= 1) {
                    ten--;
                    five--;
                }
                else if( five >= 3)
                    five -= 3;
                else
                    return false;
            }
        }
        return true;
    }
}
```
```C []
//C Code
bool lemonadeChange(int* bills, int billsSize) {
    int five = 0, ten = 0;
        for(int i = 0; i < billsSize; i++) {
            int bill = bills[i];
            if( bill == 5)
                five++;
            
            else if( bill == 10) {
                if( five >= 1) {
                    five--;
                    ten++;
                }
                else
                    return false;
            }
            else {
                if(five >= 1 && ten >= 1) {
                    ten--;
                    five--;
                }
                else if( five >= 3)
                    five -= 3;
                else
                    return false;
            }
        }
        return true;
}
```
```Python []
#Python Code
class Solution(object):
    def lemonadeChange(self, bills):
        """
        :type bills: List[int]
        :rtype: bool
        """
        f , t = 0, 0
        for bill in bills:
            if bill == 5:
                f += 1
            elif bill == 10:
                if f >= 1:
                    f -= 1
                    t += 1
                else:
                    return False
            else:
                if f >= 1 and t >= 1:
                    t -= 1
                    f -= 1
                elif f >= 3:
                    f -= 3
                else:
                    return False
        return True
    
        
```
```C# []
//C# Code
public class Solution {
    public bool LemonadeChange(int[] bills) {
        int five = 0, ten = 0;
        foreach(var bill in bills) {
            if( bill == 5)
                five++;
            
            else if( bill == 10) {
                if( five >= 1) {
                    five--;
                    ten++;
                }
                else
                    return false;
            }
            else {
                if(five >= 1 && ten >= 1) {
                    ten--;
                    five--;
                }
                else if( five >= 3)
                    five -= 3;
                else
                    return false;
            }
        }
        return true;
    }
}
```
``` Go []
func lemonadeChange(bills []int) bool {
    five, ten := 0, 0

    for _, bill := range bills {
        if bill == 5 {
            five++
        } else if bill == 10 {
            if five >= 1 {
                five--
                ten++
            } else {
                return false
            }
        } else {
            if five >= 1 && ten >= 1 {
                five--
                ten--
            } else if five >= 3 {
                five -= 3
            } else {
                return false
            }
        }
    }
    return true
}
```
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
     O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
     O(1)
# Code
![th.jpeg](https://assets.leetcode.com/users/images/8f73790c-f787-4f38-81ca-a03e3c79bfaa_1723719231.3887203.jpeg)

['Array', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']