489, lemonade change

Insert picture description here

If you want to see more algorithm questions, you can scan the QR code above to follow my WeChat official account " Data Structure and Algorithms ". Up to now, I have updated more than 500 algorithm questions in the official account , some of which have been sorted into pdf documents. , As of now, there are more than 800 pages in total (and will continue to increase), you can reply to the keyword "pdf" in the official account to download.


Problem Description

At the lemonade stand, each glass of lemonade sells for $5.

Customers line up to buy your products, (in the order of bills paid) one cup at a time.

Each customer only buys a glass of lemonade, and then pay you $5, $10 or $20 . You must give each customer the correct change, which means that the net transaction is that each customer pays you $5.


Note that you don't have any change in hand at the beginning.
If you can give each customer the correct change, return true, otherwise return false.


Example 1:

Input : [5,5,5,10,20]

Output : true

Explanation :

From the first 3 customers, we collect 3 5 USD bills in order.

From the 4th customer, we received a $10 bill and returned $5.

For the fifth customer, we returned a 10 dollar bill and a 5 dollar bill.

Since all customers got the correct change, we output true.

Example 2:

Input : [5,5,10]

Output : true

Example 3:

Input : [10,10]

Output : false

Example 4:

Input : [5,5,10,10,20]

Output : false

Explanation :

From the first 2 customers, we collect 2 US$5 bills in order.

For the next 2 customers, we charge a $10 bill and return $5.

For the last customer, we cannot return $15 because we now only have two $10 bills.

Since not every customer gets the correct change, the answer is false.


prompt:

  • 0 <= bills.length <= 10000

  • bills[i] is either 5 or 10 or 20


problem analysis

This question can be regarded as a very common question in life. For every customer, if we have enough change to give him change, then we return true, as long as a customer does not have enough change to give him change, we return false.


Customers can only have 3 kinds of banknotes , 5 yuan, 10 yuan, and 20 yuan . We need to count the amount of 5 yuan and 10 yuan, and 20 yuan does not need to be counted, because 20 yuan can’t be given to others.


  • The customer gives 5 yuan, plus 1 for the amount of 5 yuan

  • The customer gives 10 yuan, and the amount of 5 yuan is subtracted by 1 (after the reduction, the amount of 5 yuan will be judged. If it is less than 0, it means that the 5 yuan is not enough, and the customer cannot be changed. Return false directly)

  • The customer gives 20 yuan. According to the common sense of life, if there is a 10 yuan, you should first find him 10 yuan, and then find him a 5 yuan. If there is no 10 yuan, find him 3 5 yuan, and then judge the amount of 5 yuan, if it is less than 0, return false directly.

The principle is relatively simple, let's look at the code

public boolean lemonadeChange(int[] bills) {
    
    
    //统计店员所拥有的5元和10元的数量(20元的不需要统计,
    //因为顾客只能使用5元,10元和20元,而20元是没法
    // 给顾客找零的)
    int five = 0, ten = 0;
    for (int bill : bills) {
    
    
        if (bill == 5) {
    
    
            //如果顾客使用的是5元,不用找零,5元数量加1
            five++;
        } else if (bill == 10) {
    
    
            //如果顾客使用的是10元,需要找他5元,所以
            //5元数量减1,10元数量加1
            five--;
            ten++;
        } else if (ten > 0) {
    
    
            //否则顾客使用的只能是20元,顾客使用20元的时候,
            //如果我们有10元的,要尽量先给他10元的,然后再
            //给他5元的,所以这里5元和10元数量都要减1
            ten--;
            five--;
        } else {
    
    
            //如果顾客使用的是20元,而店员没有10元的,
            //就只能给他找3个5元的,所以5元的数量要减3
            five -= 3;
        }

        //上面我们找零的时候并没有判断5元的数量,如果5元的
        //数量小于0,说明上面某一步找零的时候5元的不够了,
        //也就是说没法给顾客找零,直接返回false即可
        if (five < 0) {
    
    
            return false;
        }
    }
    return true;
}

to sum up

A common sense question in life, when we found change, we did not first judge the amount of 5 yuan, and after the change, we judge whether the amount of 5 yuan is greater than 0 or less than 0. If you first judge the amount of 5 yuan when making change, it's just a little troublesome, because customers must judge first as long as they don't give 5 yuan.

Guess you like

Origin blog.csdn.net/abcdef314159/article/details/112298760