860. Lemonade Change

class Solution { public: bool lemonadeChange(vector<int>& bills) { int fives = 0; int tens = 0; // how many five bills and ten bills we have for (int b : bills){ if (b == 5) fives++; else if (b == 10){ if (fives != 0){ fives--; tens++; } else return false; } else { // b = 20 if (tens >= 1 & fives >= 1){ tens--; fives--; } else if (fives >= 3){ // have to give three fives fives -= 3; } else return false; } } return true; } };
Greedy Algorithm...

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.