Friday, January 4, 2019

860. Lemonade Change

Version #2
48.20 %
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) {
countTen++;
countFive--;
} else if (countTen > 0) { // 20
countTen--;
countFive--;
} else {
countFive -= 3;
}
if (countFive < 0) return false;
}
return true;
}
}


Version #1
48.20 %
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 (countTen > 0) {
countTen--;
bill -= 10;
}
while (bill > 5) {
if (countFive <= 0) return false;
countFive--;
bill -= 5;
}
}
}
return true;
}
}

No comments:

Post a Comment