LeetCode860. 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 do not have any change in hand at first.

Given an integer array bills where billsi is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

Example 1:

Input: bills = 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: bills = 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 not give the change of 15 back because we only have two 10 bills.

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

Constraints:

1 <= bills.length <= 105

billsi is either 5, 10, or 20.

二、题解

cpp 复制代码
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int n = bills.size();
        vector<int> balance(3,0);
        for(int i = 0;i < n;i++){
            int sub = bills[i] - 5;
            if(sub == 5 && balance[0] == 0) return false;
            else if(sub == 15 && (balance[0] == 0 || (balance[1] == 0 && balance[0] < 3))) return false;
            if(sub == 5) balance[0]--;
            else if(sub == 15){
                if(balance[1] > 0) balance[0]--,balance[1]--;
                else balance[0] -= 3;
            }
            if(bills[i] == 5) balance[0]++;
            else if(bills[i] == 10) balance[1]++;
            else balance[2]++;
        }
        return true;
    }
};

更好的解法

cpp 复制代码
class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int n = bills.size();
        int five = 0,ten = 0,twenty = 0;
        for(int i = 0;i < n;i++){
            if(bills[i] == 5) five++;
            else if(bills[i] == 10){
                if(five == 0) return false;
                ten++;
                five--;
            }
            else if(bills[i] == 20){
                if(five > 0 && ten > 0) five--,ten--;
                else if(five >= 3 && ten == 0) five -= 3;
                else return false;
            }
        }
        return true;
    }
};
相关推荐
维克兜率天几秒前
4.1.3 策略类型全景图:六大策略,你适合哪个
笔记·python·算法·量化
jimy12 分钟前
std::move(a)本质是把a转换成右值引用类型
开发语言·c++
01_ice5 分钟前
c++类和对象(中)
c++·算法
纪念 2298 分钟前
数据结构排序(一)
数据结构·算法·排序算法
码匠许师傅10 分钟前
【设计模式精讲】4.单例模式(Singleton)
c++·单例模式·设计模式
薛定e的猫咪11 分钟前
(NeurIPS 2021)MatNet:面向矩阵型关系数据的神经组合优化编码器
人工智能·深度学习·线性代数·算法·矩阵
wind1003219 分钟前
VC++ 运行库 vcredist 安装失败|CLR 80131522 报错完整修复
开发语言·c++·游戏
高亦真25 分钟前
今天是学习嵌入式的第32天
linux·学习·算法
OPEN-F7 小时前
C++进阶教程:继承与多态
开发语言·c++
鹿角片ljp9 小时前
LeetCode 236. 二叉树的最近公共祖先|递归后序
算法