问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. var bills = new int[] { 5, 5, 5, 10, 20 };
  4. var res = LemonadeChange(bills);
  5. Console.WriteLine(res);
  6. bills = new int[] { 5, 5, 10, 10, 20 };
  7. res = LemonadeChange2(bills);
  8. Console.WriteLine(res);
  9. bills = new int[] { 5, 10 };
  10. res = LemonadeChange3(bills);
  11. Console.WriteLine(res);
  12. Console.ReadKey();
  13. }
  14. public static bool LemonadeChange(int[] bills) {
  15. //统计5美元和10美元的数量
  16. var five = 0;
  17. var ten = 0;
  18. for(var i = 0; i < bills.Length; i++) {
  19. if(bills[i] == 5) {
  20. //5美元,则5美元+1
  21. five++;
  22. } else if(bills[i] == 10) {
  23. //10美元,则10美元+1,找零5美元,5美元-1
  24. ten++;
  25. five--;
  26. } else if(bills[i] == 20) {
  27. //20美元时,尽量先找零10美元和5美元
  28. //然后考虑找零3个5美元
  29. if(ten > 0) {
  30. ten--;
  31. five--;
  32. } else {
  33. five -= 3;
  34. }
  35. }
  36. //5美元或10美元小于0时,说明至少有1次找零是失败的
  37. //直接返回 false
  38. if(five < 0 || ten < 0) return false;
  39. }
  40. //最后返回 true
  41. return true;
  42. }
  43. public static bool LemonadeChange2(int[] bills) {
  44. //基本思路同 LemonadeChange,提供了一个不同的实现
  45. var list = new List<int>();
  46. for(var i = 0; i < bills.Length; i++) {
  47. if(bills[i] == 5) {
  48. list.Add(5);
  49. } else if(bills[i] == 10) {
  50. list.Add(10);
  51. if(!list.Remove(5)) return false;
  52. } else if(bills[i] == 20) {
  53. if(list.Remove(10)) {
  54. if(!list.Remove(5)) return false;
  55. } else {
  56. for(var j = 0; j < 3; j++) {
  57. if(!list.Remove(5)) return false;
  58. }
  59. }
  60. }
  61. }
  62. return true;
  63. }
  64. public static bool LemonadeChange3(int[] bills) {
  65. //不建议的解法,仅提供思路
  66. //基本思路同 LemonadeChange,提供了一个不同的实现
  67. try {
  68. var five = new Stack<int>();
  69. var ten = new Stack<int>();
  70. for(var i = 0; i < bills.Length; i++) {
  71. if(bills[i] == 5) {
  72. five.Push(5);//这个值是什么无所谓
  73. } else if(bills[i] == 10) {
  74. ten.Push(10);//这个值是什么无所谓
  75. five.Pop();
  76. } else if(bills[i] == 20) {
  77. if(ten.Any()) {
  78. ten.Pop();
  79. five.Pop();
  80. } else {
  81. for(var j = 0; j < 3; j++) {
  82. five.Pop();
  83. }
  84. }
  85. }
  86. }
  87. return true;
  88. } catch(Exception ex) {
  89. return false;
  90. }
  91. }
  92. }

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

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

  1. True
  2. False
  3. 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. UnsupportedClassVersionError的错误处理

    造成这种错误的原因是支持Tomcat运行的JDK版本与支持application运行的JDK版本不一致导致的. 解决办法: 将JDK版本改成一致. 步骤 1.Window ——> Prefere ...

  2. Python实现性能自动化测试竟然如此简单【颠覆你的三观】

    一.思考 1.什么是性能自动化测试?   性能系统负载能力 超负荷运行下的稳定性 系统瓶颈 自动化测试使用程序代替手工 提升测试效率 性能自动化使用代码模拟大批量用户 让用户并发请求 多页面多用户并发 ...

  3. Ethical Hacking - GAINING ACCESS(3)

    Sever side attacks code execution Let‘s analyze the Zenmap scan result first and search for somethin ...

  4. 动手实现一个较为简单的MQTT服务端和客户端

    项目地址:https://github.com/hnlyf168/DotNet.Framework 昨天晚上大致测试了下 ,490个客户端(一个收一个发)  平均估计每个每秒60个包  使用mqtt协 ...

  5. DJANGO-天天生鲜项目从0到1-012-订单-用户订单页面

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  6. 图片懒加载、ajax异步调用数据、lazyload插件的使用

    关于这个效果还是很简单的,样式部分我就不多说了,我就简单的写了一下布局, 这是css样式 我们先说一下实现的原理. 我们都知道在于图片的引入,我们都是用src来引入图片地址.从而实现图片的显示.那我们 ...

  7. assemble、compile、make、build和rebuild的关系

    assemble:打包(之前已经编译了源文件)compile.make.build和rebuild都是编译过程:将源代码转换为可执行代码的过程,Java的编译会将java编译为class文件,将非ja ...

  8. javascript中的堆栈、深拷贝和浅拷贝、闭包

    堆栈 在javascript中,堆内存是用来存放引用类型的空间环境 而栈内存,是存储基本类型和指定代码的环境 在对象中的属性名具有唯一性,数字属性名=字符串属性名,但是在测试的时候你会发现,好像所有属 ...

  9. lua 表

    最近在尝试配置 awesome WM,因此粗略地学习了一下 lua . 在学习过程中,我完全被表表在 lua 中的应用所镇住了. 表在 lua 中真的是无处不在:首先,它可以作为字典和数组来用:此外, ...

  10. mysql字符集 utf8 和utf8mb4 的区别

    一.导读我们新建mysql数据库的时候,需要指定数据库的字符集,一般我们都是选择utf8这个字符集,但是还会又一个utf8mb4这个字符集,好像和utf8有联系,今天就来解析一下这两者的区别. 二.起 ...