Topic Tags: Greedy
When the time to receive the $ 5, Five count;
When you receive $ 10 of time, came with five zero;
When you receive $ 20 when the first came with ten to zero, if not, then five come to zero.
If the process, five less than 0, indicating not enough money to give change, fail.
Java Solution:
Runtime: 2 ms, faster than 81.80%
Memory Usage: 42.1 MB, less than 7.69%
Completion Date: 02/15/2020
Key: setting five, ten counting
class Solution { public boolean lemonadeChange(int[] bills) { int five = 0, ten = 0; for(int b : bills) { if(b == 5) { five++; } else if(b == 10) { five--; ten++; } else if(b == 20) { if(ten > 0) { // use ten first ten--; five--; } else { five -= 3; } } if(five < 0) return false; } return true; } }
Reference: LeetCode discuss
LeetCode title list - LeetCode Questions List
Topic Source: https: //leetcode.com/