问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Python Ethical Hacking - Malware Analysis(4)

    DOWNLOAD_FILE Download files on a system. Once packaged properly will work on all operating systems. ...

  2. JAVA学习过程中遇到的BUG

    Java异常 1.NullPointException java.lang.NullPointException,就是我们经常遇到的空指针异常. java是没有指针的,这里说的"java指针 ...

  3. Python 脚本语言

    python 脚本语言(python的命名起源于一个脚本screenplay,每次运行都会使对话框逐字重复.由著名的“龟叔”Guido van Rossum在1989年圣诞节期间编写.) Python ...

  4. 自学 Python 到什么程度能找到工作,1300+ 条招聘信息告诉你答案

    随着移动互联网的发展以及机器学习等热门领域带给人们的冲击,让越来越多的人接触并开始学习 Python.无论你是是科班出身还是非科班转行,Python 无疑都是非常适合你入门计算机世界的第一门语言,其语 ...

  5. python 批量重命名文件名字

    import os print(os.path) img_name = os.listdir('./img') for index, temp_name in enumerate(img_name): ...

  6. python3的字符串常用方法

    find()# 方法 find()# 范围查找子串,返回索引值,找不到返回-1 # 语法 s.find(substring, start=0, end=len(string)) # 参数 # subs ...

  7. nginx--做为负载均衡使用

    在之前的文章中,我们通过服务代理的方式已经看到了Nginx有作为负载均衡服务的功能了,在这篇文章中,我会讲解Nginx的基本的负载均衡的使用.backup状态演示.轮询策略和加权轮询.负载均衡策略ip ...

  8. JPA第一天

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"springdata"获取视频和教程资料! b站在线视 ...

  9. luogu P5410 模板 扩展 KMP Z函数 模板

    LINK:P5410 模板 扩展 KMP Z 函数 画了10min学习了一下. 不算很难 思想就是利用前面的最长匹配来更新后面的东西. 复杂度是线性的 如果不要求线性可能直接上SA更舒服一点? 不管了 ...

  10. springboot集成mongo

    大家可以关注我的微信公众号“秦川以北” 后续更多精彩实用内容分享 ​在项目中配置,mongoDB数据库,spring整合 1. 引入pom依赖 <dependency> <group ...