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;
    }
};
全部评论

相关推荐

想去毕业旅行的斑马在...:学校不是92的话,没有实习经历投不了大厂,去投中小厂,拿点实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
2950次浏览 42人参与
# HR最不可信的一句话是__ #
985次浏览 32人参与
# 巨人网络春招 #
11477次浏览 224人参与
# 春招至今,你的战绩如何? #
14489次浏览 135人参与
# AI面会问哪些问题? #
874次浏览 21人参与
# 你的实习产出是真实的还是包装的? #
2588次浏览 52人参与
# MiniMax求职进展汇总 #
24829次浏览 321人参与
# 沪漂/北漂你觉得哪个更苦? #
1076次浏览 29人参与
# 你做过最难的笔试是哪家公司 #
1084次浏览 20人参与
# AI时代,哪个岗位还有“活路” #
2630次浏览 49人参与
# XX请雇我工作 #
51141次浏览 171人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7950次浏览 43人参与
# 简历第一个项目做什么 #
32035次浏览 357人参与
# 简历中的项目经历要怎么写? #
310850次浏览 4257人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152795次浏览 888人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187527次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64474次浏览 860人参与
# 如果重来一次你还会读研吗 #
229960次浏览 2011人参与
# 投格力的你,拿到offer了吗? #
178175次浏览 889人参与
# 你怎么看待AI面试 #
180611次浏览 1291人参与
# 正在春招的你,也参与了去年秋招吗? #
364105次浏览 2640人参与
# 腾讯音乐求职进展汇总 #
160808次浏览 1114人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务