柠檬水找零(贪心算法)


public class Main{
    public static void main(String[] args) {
        System.out.println(change(new int[]{5, 5,10,20}));
    }

    private static boolean change(int[] bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            if (bill == 5) {
                five++;
            } else if (bill == 10) {
                if (five == 0) {
                    return false;
                }
                five--;
                ten++;
            } else {
                if (five > 0 && ten > 0) {
                    five--;
                    ten--;
                } else if (five >= 3) {
                    five -= 3;
                }else {
                    return  false;
                }

            }
        }
            return  true;
    }
}

你可能感兴趣的:(算法,java,算法,贪心)