问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

在柠檬水摊上,每一杯柠檬水的售价为 5 美元。

顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。

每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。

注意,一开始你手头没有任何零钱。

如果你能给每位顾客正确找零,返回 true ,否则返回 false 。

输入:[5,5,5,10,20]

输出:true

解释:前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。由于所有客户都得到了正确的找零,所以我们输出 true。

输入:[5,5,10]

输出:true

输入:[10,10]

输出:false

输入:[5,5,10,10,20]

输出:false

解释:前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。由于不是每位顾客都得到了正确的找零,所以答案是 false。

提示:

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

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.

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.

Input: [5,5,10]

Output: true

Input: [10,10]

Output: false

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.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

public class Program {

    public static void Main(string[] args) {
var bills = new int[] { 5, 5, 5, 10, 20 }; var res = LemonadeChange(bills);
Console.WriteLine(res); bills = new int[] { 5, 5, 10, 10, 20 }; res = LemonadeChange2(bills);
Console.WriteLine(res); bills = new int[] { 5, 10 }; res = LemonadeChange3(bills);
Console.WriteLine(res); Console.ReadKey();
} public static bool LemonadeChange(int[] bills) {
//统计5美元和10美元的数量
var five = 0;
var ten = 0;
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
//5美元,则5美元+1
five++;
} else if(bills[i] == 10) {
//10美元,则10美元+1,找零5美元,5美元-1
ten++;
five--;
} else if(bills[i] == 20) {
//20美元时,尽量先找零10美元和5美元
//然后考虑找零3个5美元
if(ten > 0) {
ten--;
five--;
} else {
five -= 3;
}
}
//5美元或10美元小于0时,说明至少有1次找零是失败的
//直接返回 false
if(five < 0 || ten < 0) return false;
}
//最后返回 true
return true;
} public static bool LemonadeChange2(int[] bills) {
//基本思路同 LemonadeChange,提供了一个不同的实现
var list = new List<int>();
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
list.Add(5);
} else if(bills[i] == 10) {
list.Add(10);
if(!list.Remove(5)) return false;
} else if(bills[i] == 20) {
if(list.Remove(10)) {
if(!list.Remove(5)) return false;
} else {
for(var j = 0; j < 3; j++) {
if(!list.Remove(5)) return false;
}
}
}
}
return true;
} public static bool LemonadeChange3(int[] bills) {
//不建议的解法,仅提供思路
//基本思路同 LemonadeChange,提供了一个不同的实现
try {
var five = new Stack<int>();
var ten = new Stack<int>();
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
five.Push(5);//这个值是什么无所谓
} else if(bills[i] == 10) {
ten.Push(10);//这个值是什么无所谓
five.Pop();
} else if(bills[i] == 20) {
if(ten.Any()) {
ten.Pop();
five.Pop();
} else {
for(var j = 0; j < 3; j++) {
five.Pop();
}
}
}
}
return true;
} catch(Exception ex) {
return false;
}
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

True
False
True

分析:

显而易见,以上3种算法的时间复杂度均为:  。

C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)的更多相关文章

  1. [Swift]LeetCode860. 柠檬水找零 | Lemonade Change

    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...

  2. Leetcode 860. 柠檬水找零

    860. 柠檬水找零  显示英文描述 我的提交返回竞赛   用户通过次数187 用户尝试次数211 通过次数195 提交次数437 题目难度Easy 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...

  3. 【LeetCode】860. 柠檬水找零

    860. 柠檬水找零 知识点:贪心 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 ...

  4. LeetCode 860.柠檬水找零(C++)

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

  5. LeetCode 860. 柠檬水找零 (贪心)

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

  6. [LeetCode] 860. 柠檬水找零 lemonade-change(贪心算法)

    思路: 收到5块时,只是添加:收到十块时,添加10块,删除一个5块:收到20块时,添加20,删除一个10块一个5块,或者直接删除3个5块(注意:这里先删除5+10优于3个5) class Soluti ...

  7. LeetCode:柠檬水找零【860】

    LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...

  8. LeetCode.860-卖柠檬水找零(Lemonade Change)

    这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...

  9. Leetcode860.Lemonade Change柠檬水找零

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

随机推荐

  1. 系统部署时的Could not load file or assembly 'Microsoft.VisualStudio.Enterprise.ASPNetHelper问题

    在web.config中,如下代码段 <compilation debug="true" targetFramework="4.0" assemblyPo ...

  2. Burp Suite Decoder Module - 解码模块

    官方参考链接:https://portswigger.net/burp/documentation/desktop/tools/decoder 该模块主要进行编码和解码,支持编码方式有:Plain,U ...

  3. js中实现继承的方法

    目录 借用构造函数 组合继承 原型式继承 寄生式继承 寄生组合式继承 借用构造函数 这种技术的基本思想很简单,就是在子类型构造函数的内部调用超类型的构造函数.另外,函数只不过是在特定环境中执行代码的对 ...

  4. Lucas定理 & Catalan Number & 中国剩余定理(CRT)

    又双叒叕来水数论了 今天来学习\(Lucas \:\ \& \:\ Catalan Number\) 两者有着密切的联系(当然还有CRT),所以放在一起学习一下 \(Lucas\) 定义\(\ ...

  5. 使用Thanos实现Prometheus指标联邦

    本文来自Rancher Labs Prometheus是CNCF中已经毕业的项目之一,主要用于监控和告警.在Kubernetes生态中,它是应用最为广泛的监控和告警工具之一.Rancher用户可以通过 ...

  6. navcat 增删改查

    navcat // sql 语句结尾的地方需要 设置为空 SELECT * FROM users ; UPDATE users SET uname = 'hezhi' WHERE uid = 3 // ...

  7. CentOS 下的验证码显示问题

    开发环境 AND 生产环境.gif 问题: 项目部署到 CentOS 的服务器后,图片验证码请求时出现 500 错误, 日志一直是 ArrayIndexOfBoundsException:0,数组第 ...

  8. Ross Girshick讲解如何writing good research papers

    ICCV 2019上,Facebook AI 的 Ross Girshick 做了一个关于目标检测和实例分割的 tutorial,最后用19页PPT讲解了如何 writing good researc ...

  9. 图解Kubernetes——故障排查指南

    针对越来多的Kubernetes容器云,对Kubernetes集群的故障排查却成了一个棘手问题.本文虫虫给大家以直观图示方式介绍如何排查Kubernetes的故障.该篇是系列文章续——故障排查篇. 概 ...

  10. Web压测工具之Webbench和http_load

    Webbench简介 是知名的网站压力测试工具,能测试处在相同硬件上,不同服务的性能以及不同硬件上同一个服务的运行状况. webbench的标准测试可以向我们展示服务器的两项内容:每秒钟相应请求数和每 ...