LeetCode – 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 don't have any change in hand at first. Return true if and only if you can provide every customer with correct change. Example 1: 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.
Example 2: Input: [5,5,10]
Output: true
Example 3: Input: [10,10]
Output: false
Example 4: 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.
这是一个关于找零的问题。首先,因为初始没有资本,而且每份柠檬水的售价是5美元。因此,5美元是最“珍贵”的找零资源。
如果顾客给的是10美元,那么只能找给他一张5美元的。
如果顾客给的是20美元,因为5美元比较稀缺,因此如果有10美元的钞票,则优先找给他10美元,然后再找一张5美元;否则的话,就直接给三张5美元的钞票。
如果5美元的数量变为“负”,说明找零失败,返回False;否则就返回True。
class Solution {
public boolean lemonadeChange(int[] bills) {
Map<Integer, Integer> map = new HashMap<>();
map.put(5,0);
map.put(10,0);
for(int bill : bills){
if(bill == 5){
map.put(5, map.get(5)+1);
}
else if(bill == 10){
if(map.get(5) == 0){
return false;
}
map.put(5,map.get(5)-1);
map.put(10,map.get(10)+1);
}
else if(bill == 20){
if(map.get(5) == 0){
return false;
}
if(map.get(10) == 0 && map.get(5) < 3){
return false;
}
if(map.get(10) > 0 && map.get(5) == 0){
return false;
}
if(map.get(10) > 0){
map.put(10, map.get(10)-1);
map.put(5, map.get(5)-1);
}
else{
map.put(5,map.get(5)-3);
}
}
}
return true;
}
}
LeetCode – Lemonade Change的更多相关文章
- [LeetCode] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- 【Leetcode_easy】860. Lemonade Change
problem 860. Lemonade Change solution class Solution { public: bool lemonadeChange(vector<int> ...
- 【LeetCode】860. Lemonade Change 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 860. Lemonade Change
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4036 访问. 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...
- LeetCode.860-卖柠檬水找零(Lemonade Change)
这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...
- LeetCode题解之Lemonade Change
1.题目描述 2.问题分析 使用贪心算法. 3.代码 class Solution { public: bool lemonadeChange(vector<int>& bills ...
- [Swift]LeetCode860. 柠檬水找零 | Lemonade Change
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- C#中使用FFMPEG切割、合并视频。
参考网址:https://blog.csdn.net/samwang_/article/details/70332924 使用前先确保电脑已经安装了FFMPEG,并且配置好环境变量.检测是否安装配置好 ...
- .NET接入UnionPay银联支付(一)手机wap支付
最近呢,比较忙,公司在接入银联全渠道支付,博主接手的wap支付,发表一下博主在接入的时候遇到的坑和注意事项,方便大家学习接入,爬坑的路上更顺利一点~ 开发步骤 1. 以表单的方式组装要发送给银联全渠道 ...
- Linux运维工程师需要掌握什么才能胜任工作呢
万丈高楼平地起,所有一切的高深的技术都离不开最基本的技术,那么作为运维工程师的你,什么是最基本的技术呢,毫无疑问是Linux,Linux 是你所有一切技术的根源,试想一下如果你连基础的操作命令都不知道 ...
- mybatis输出sql语句
方法一: 这种方法是mybatis官网上介绍的,比较好用: log4j.properties: log4j.rootLogger=ERROR,consolelog4j.appender.console ...
- windows7环境下java jdk的配置
第一步: 肯定是先下载好java jdk啦~~ 网址在这里:http://www.oracle.com/technetwork/java/javase/downloads/index.html 打开这 ...
- centos7安装配置zabbix4.0
zabbix01 198.8.8.211 zabbix-server4.0 zabbix02 198.8.8.212 zabbix-agent4.0 一:zabbix服务端环境 ...
- centos7安装配置tomcat
第一步:下载Tomcat8压缩包 进入 http://tomcat.apache.org/download-80.cgi 下载tar.gz压缩包 第二步:用ftp工具把压缩包上传到/home/data ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...
- JAVA测试编程
本周我们上JAVA课的时候进行了一次测试,本次测试以模拟中国工商银行自助机ATM的功能为大致要求,要求我们进行编写调试,以求达到试题要求. 测试要求我们能模拟ATM的功能设计,当用户插卡后显示,输入密 ...
- Python 静态方法
class Person: @staticmethod # 静态方法 def yue(): print("fsadf") # 静态方法可以使用对象访问. 也可以使用类名访问. 但是 ...