Skip to main content

860. Lemonade Change

class Solution {
public:
bool lemonadeChange(vector<int>& bills)
{
int coin5 = 0;
int coin10 = 0;

for (const auto& b : bills)
{
if (b == 5)
{
coin5++;
}
else if (b == 10)
{
if (coin5 > 0)
{
coin5--;
coin10++;
}
else return false;
}
else if (b == 20)
{
if (coin10 > 0 && coin5 > 0)
{
coin5--;
coin10--;
}
else if (coin5 >= 3)
{
coin5 -= 3;
}
else return false;
}
}
return true;
}
};
  • T: O(n)O(n)
  • S: O(1)O(1)