《LeetCode力扣练习》代码随想录——贪心算法(柠檬水找零—Java)
刷题思路来源于 代码随想录
860. 柠檬水找零
-
贪心
class Solution { public boolean lemonadeChange(int[] bills) { int five = 0; int ten = 0; for (int num : bills) { if (num == 5) { five++; } else if (num == 10) { if (five >= 1) { five--; ten++; } else { return false; } } else { if (ten >= 1 && five >= 1) { ten--; five--; } else if (five >= 3) { five -= 3; } else { return false; } } } return true; } }