g0801_0900.s0860_lemonade_change.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0801_0900.s0860_lemonade_change;
// #Easy #Array #Greedy
public class Solution {
public boolean lemonadeChange(int[] bills) {
int countFive = 0;
int countTen = 0;
for (int bill : bills) {
if (bill == 5) {
countFive++;
} else if (bill == 10) {
if (countFive == 0) {
return false;
}
countFive--;
countTen++;
} else if (bill == 20) {
if (countFive > 0 && countTen > 0) {
countFive--;
countTen--;
} else if (countFive >= 3) {
countFive -= 3;
} else {
return false;
}
}
}
return true;
}
}