LeetCode #860 Lemonade Change 檸檬水找零

860 Lemonade Change 檸檬水找零

Description:
At a lemonade stand, each lemonade costs $5.

Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).

Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.

Note that you don't have any change in hand at first.

Return true if and only if you can provide every customer with correct change.

Example:

Example 1:

Input: [5,5,5,10,20]
Output: true
Explanation:
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got 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 two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can't give change of $15 back because we only have two $10 bills.
Since not every customer received correct change, the answer is false.

Note:

0 <= bills.length <= 10000
bills[i] will be either 5, 10, or 20.

題目描述:
在檸檬水?dāng)偵?,每一杯檸檬水的售價(jià)為 5 美元。

顧客排隊(duì)購買你的產(chǎn)品,(按賬單 bills 支付的順序)一次購買一杯。

每位顧客只買一杯檸檬水,然后向你付 5 美元、10 美元或 20 美元。你必須給每個(gè)顧客正確找零,也就是說凈交易是每位顧客向你支付 5 美元。

注意,一開始你手頭沒有任何零錢。

如果你能給每位顧客正確找零,返回 true ,否則返回 false 。

示例 :

示例 1:

輸入:[5,5,5,10,20]
輸出:true
解釋:
前 3 位顧客那里,我們按順序收取 3 張 5 美元的鈔票。
第 4 位顧客那里,我們收取一張 10 美元的鈔票,并返還 5 美元。
第 5 位顧客那里,我們找還一張 10 美元的鈔票和一張 5 美元的鈔票。
由于所有客戶都得到了正確的找零,所以我們輸出 true。

示例 2:

輸入:[5,5,10]
輸出:true

示例 3:

輸入:[10,10]
輸出:false

示例 4:

輸入:[5,5,10,10,20]
輸出:false
解釋:
前 2 位顧客那里,我們按順序收取 2 張 5 美元的鈔票。
對(duì)于接下來的 2 位顧客,我們收取一張 10 美元的鈔票,然后返還 5 美元。
對(duì)于最后一位顧客,我們無法退回 15 美元,因?yàn)槲覀儸F(xiàn)在只有兩張 10 美元的鈔票。
由于不是每位顧客都得到了正確的找零,所以答案是 false。

提示:

0 <= bills.length <= 10000
bills[i] 不是 5 就是 10 或是 20

思路:

整體使用貪心算法, 從 10開始找零, 不夠的用 5找零(檸檬水是真的貴)
因?yàn)橐婚_始沒有零錢, 只要第一個(gè)人沒有給 5直接返回false
設(shè)置兩個(gè)變量 five和 ten代表收到的錢的數(shù)量
10的只能用 5找零
20的可以用 10 + 5或者 5 * 3找零
時(shí)間復(fù)雜度O(n), 空間復(fù)雜度O(1)

代碼:
C++:

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

Java:

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

Python:

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        if bills[0] != 5:
            return False
        five, ten= 0, 0
        for bill in bills:
            if bill == 5:
                five += 1
            if bill == 10:
                if not five:
                    return False
                five -= 1
                ten += 1
            if bill == 20:
                if ten and five:
                    five -= 1
                    ten -= 1
                elif five > 2:
                    five -= 3
                else:
                    return False
        return True
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容