LeetCode: 860. Lemonade Change

LeetCode: 860. Lemonade Change

题目描述

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 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.

解题思路 —— 贪心思想

优先选择找大面额的钞票,如果遇到找不开的情况,则认为其无法找开。

AC 代码

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int cnt[3] = {0};  // 0, 1, 2 ==> 5, 15, 20

        for(int i = 0; i < bills.size(); ++i)
        {
            if(bills[i] == 5)
            {
                ++cnt[0];
            }
            else if(bills[i] == 10) // 找 5 块
            {
                ++cnt[1];
                --cnt[0];
            }
            else if(bills[i] == 20) // 找 15 块
            {
                ++cnt[2];
                if(cnt[1] > 0) // 优先找 10 + 5
                {
                    --cnt[1]; 
                    --cnt[0];
                }
                else // 没有 10 块,直接找5块
                {
                    cnt[0] -= 3;
                }
            }

            if(cnt[0] < 0) // 剩余 5 块钱面额钞票数量为负,则找不开
            {
                return false;
            }
        }

        return true;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
3637次浏览 45人参与
# 离家近房租贵VS离家远但房租低,怎么选 #
16869次浏览 137人参与
# 米连集团26产品管培生项目 #
7233次浏览 226人参与
# 春招至今,你的战绩如何? #
15489次浏览 144人参与
# 你的实习产出是真实的还是包装的? #
2934次浏览 52人参与
# 沪漂/北漂你觉得哪个更苦? #
1413次浏览 40人参与
# 巨人网络春招 #
11519次浏览 224人参与
# HR最不可信的一句话是__ #
1066次浏览 32人参与
# AI面会问哪些问题? #
926次浏览 22人参与
# 你做过最难的笔试是哪家公司 #
1201次浏览 21人参与
# AI时代,哪个岗位还有“活路” #
2781次浏览 51人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152881次浏览 889人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7990次浏览 43人参与
# XX请雇我工作 #
51154次浏览 171人参与
# 简历第一个项目做什么 #
32120次浏览 359人参与
# 简历中的项目经历要怎么写? #
310994次浏览 4262人参与
# 投格力的你,拿到offer了吗? #
178311次浏览 891人参与
# 你最满意的offer薪资是哪家公司? #
76961次浏览 375人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187576次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64632次浏览 871人参与
# 如果重来一次你还会读研吗 #
230002次浏览 2011人参与
# 正在春招的你,也参与了去年秋招吗? #
364304次浏览 2641人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务