LeetCode-860. 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 <= 10000bills[i]will be either5,10, or20.
贪心 时间复杂度O(n)
public boolean lemonadeChange(int[] bills) {//贪心 my
int[] count=new int[3];//0为5,1为10,2为20,可用map代替
for (int i = 0; i < bills.length; i++) {
if(5==bills[i]){
count[0]++;
}
else if(10==bills[i]){
count[1]++;
if(0>=count[0]){
return false;
}
count[0]--;
}
else{
count[2]++;
if((0>=count[1]&&3>count[0])||(0>=count[0])){
return false;
}
if(0>=count[1]){
count[0]-=3;
}
else{
count[1]--;
count[0]--;
}
}
}
return true;
}
简洁版
public boolean lemonadeChange(int[] bills) {
int five = 0, ten = 0;
for (int i : bills) {
if (i == 5) five++;
else if (i == 10) {five--; ten++;}
else if (ten > 0) {ten--; five--;}
else five -= 3;
if (five < 0) return false;
}
return true;
}
LeetCode-860. Lemonade Change的更多相关文章
- 【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 ...
- [LeetCode] 860. Lemonade Change_Easy tag: Greedy
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- 860. Lemonade Change
class Solution { public: bool lemonadeChange(vector<int>& bills) { , ten = ; for (int i : ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- LeetCode – Lemonade Change
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- LeetCode.860-卖柠檬水找零(Lemonade Change)
这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...
- C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4036 访问. 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...
随机推荐
- Mysql系列六:(Mycat分片路由原理、Mycat常用分片规则及对应源码介绍)
一.Mycat分片路由原理 我们先来看下面的一个SQL在Mycat里面是如何执行的: , ); 有3个分片dn1,dn2,dn3, id=5000001这条数据在dn2上,id=10000001这条数 ...
- JAVA 构造器, extends[继承], implements[实现], Interface[接口], reflect[反射], clone[克隆], final, static, abstrac
记录一下: 构造器[构造函数]: 在java中如果用户编写类的时候没有提供构造函数,那么编译器会自动提供一个默认构造函数.它会把所有的实例字段设置为默认值:所有的数字变量初始化为0;所有的布尔变量设置 ...
- nginx 配置信息
主配置文件: cat /etc/nginx/nginx.conf# For more information on configuration, see:# * Official English Do ...
- SparkSQL demo
1.数据样本:data1.txt xiaoming,25,chengduxiaohua,23,beijingliuyang,16,hangzhouxiaoqiang,19,zhejiang 2.dem ...
- 保证Activity启动时每次都调用create
原文:https://stackoverflow.com/questions/41766547/run-oncreate-every-time-android-app-is-opened If you ...
- Win7/Win8安装"我们无法创建新的分区,也找不到现有的分区"的解决方法
如果你用pe启动光盘和pe启动盘.加载iso安装时遇到"我们无法创建新的分区,也找不到现有的分区"的情况.. 把iso里的boot和bootgmr以及sources复制到c盘,pe ...
- wchar_t和char转化
char* WcharToChar(const wchar_t* wp) { char *m_char; int len = WideCharToMultiByte(CP_ACP, 0, wp, wc ...
- super()方法
super()是一个调用父类的方法. super()用来解决多继承问题,直接用类名调用父类的方法在单继承中是没有问题的,但是如果使用多继承会涉及到查找顺序(MRO).重复调用等种种问题. python ...
- 使用 vm 加载文件中的数据到变量里面
.json 不能写注释, 还需要严格的双引号 或者使用 .json5 // test.db [ // name {name: 1} ] // test.js const fs = require('f ...
- 网络通信协议二之ISO/OSI参考模型
OSI介绍 >>Open System Interconnection,简称ISO/OSI RM >>是一个逻辑结构,并非一个具体的计算机设备或网络 >>任何两个遵 ...