Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool lemonadeChange(vector<int>& bills) {
  4. int x20 = 0, x10 = 0, x5 = 0;
  5. for (auto b: bills) {
  6. if (b == 20) {
  7. // Try 10, 5; then 5, 5, 5
  8. if (x10 && x5) {
  9. x10--;
  10. x5--;
  11. }
  12. else if (x5 >= 3) {
  13. x5 -= 3;
  14. }
  15. else {
  16. return false;
  17. }
  18. x20++;
  19. }
  20. else if (b == 10) {
  21. if (x5) {
  22. x5--;
  23. }
  24. else {
  25. return false;
  26. }
  27. x10++;
  28. }
  29. else {
  30. x5++;
  31. }
  32. }
  33. return true;
  34. }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement