代码随想录——860. 柠檬水找零

class Solution {
    public boolean lemonadeChange(int[] bills) {
        int five = 0;
        int ten = 0;
        int twenty = 0;
        for(int bill : bills){
            if(bill == 5){
                five++;
            }
            if(bill == 10){
                if(five <= 0){
                    return false;
                }
                ten++;
                five--;
            }
            if(bill == 20){
                if(five > 0 && ten > 0){
                    five--;
                    ten--;
                    twenty++;
                }
                else if(five >= 3){
                    five -=3;
                    twenty++;
                }else{
                    return false;
                }
            }
        }
        return true;
    }
}

你可能感兴趣的:(代码随想录,java,算法,数据结构)