C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode860. 柠檬水找零 | Lemonade Change
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- Leetcode 860. 柠檬水找零
860. 柠檬水找零 显示英文描述 我的提交返回竞赛 用户通过次数187 用户尝试次数211 通过次数195 提交次数437 题目难度Easy 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...
- 【LeetCode】860. 柠檬水找零
860. 柠檬水找零 知识点:贪心 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 ...
- LeetCode 860.柠檬水找零(C++)
在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...
- LeetCode 860. 柠檬水找零 (贪心)
在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...
- [LeetCode] 860. 柠檬水找零 lemonade-change(贪心算法)
思路: 收到5块时,只是添加:收到十块时,添加10块,删除一个5块:收到20块时,添加20,删除一个10块一个5块,或者直接删除3个5块(注意:这里先删除5+10优于3个5) class Soluti ...
- LeetCode:柠檬水找零【860】
LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...
- LeetCode.860-卖柠檬水找零(Lemonade Change)
这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...
- Leetcode860.Lemonade Change柠檬水找零
在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...
随机推荐
- 基于python的自动化测试框架搭建
滴~ 今日打卡! 好多天没来打卡了.博主最近一直在把碎片化知识转化为知识体系的过程中挣扎.Python语言.selenium.unittest框架.HTMLTestRunner框架都有所了解,也写 ...
- ASP.NET CORE之中间件-自定义异常中间件
参考资料:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1 1.一般A ...
- Linux平台下SSD的TRIM指令的最佳使用方式(不区别对待NVMe)
SSD写数据会出现什么问题 SSD读写的单位不是位,而是一个块.如果要改变这个块中的一位,首先要将整个块擦写成1,然后再写入更新的数据. 为了解决擦写块的低效,SSD的策略是将需要改写的块,读取出来, ...
- lua 表
最近在尝试配置 awesome WM,因此粗略地学习了一下 lua . 在学习过程中,我完全被表表在 lua 中的应用所镇住了. 表在 lua 中真的是无处不在:首先,它可以作为字典和数组来用:此外, ...
- 一切皆组件的Flutter,安能辨我是雄雌
从一开始接触Flutter,相信读者都会铭记一句话,那就是--一切皆组件.今天我们就来体会一下这句话的神奇魔力,我们先从实际的产品需求说起. 我们先来看一个简化的运行图: 我们要实现如上图所示的日期选 ...
- 你不知道的Java引用
什么是引用 引用就是保存着一块地址(门牌号)的对象,就像C语言的指针那样,引用可以传递某个数据的地址,如果我们想拿到某一条数据,就要先找到他的地址,然后告诉计算机我去拿这个地址的数据,最后计算机就 ...
- 深入探究JVM之垃圾回收算法实现细节
@ 目录 前言 垃圾回收算法实现细节 根节点枚举 安全点 安全区域 记忆集和卡表 写屏障 并发的可达性分析 低延迟GC Shenandoah ZGC 总结 前言 本篇紧接上文,主要讲解垃圾回收算法的实 ...
- 做完这套面试题,你才敢说懂Excel
下面的题目来自一份商品专员的面试题,其中有涉及到条件格式.自定义排序.数据验证制作下拉菜单.查找引用类函数.文本提取函数等等技能. 满满的干货技能可不是商品专员“专属”,如果你能熟练掌握,在平日工作中 ...
- SQL数据库优化总结
1.在表中建立索引优先考虑 where.group by使用到的数据. 2.查询的sql语句中不要使用select * ,因为会返回许多无用的字段降低查询的效率,应该使用具体的字段代替*,只返回使用到 ...
- pandas属性和方法
Series对象的常用属性和方法 loc[ ]和iloc[ ]格式示例表 Pandas提供的数据整理方法 Pandas分组对象的属性和方法 date_range函数的常用freq参数表