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 510, or 20.

贪心 时间复杂度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的更多相关文章

  1. 【Leetcode_easy】860. Lemonade Change

    problem 860. Lemonade Change solution class Solution { public: bool lemonadeChange(vector<int> ...

  2. 【LeetCode】860. Lemonade Change 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. [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 ...

  4. [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 ...

  5. 860. Lemonade Change

    class Solution { public: bool lemonadeChange(vector<int>& bills) { , ten = ; for (int i : ...

  6. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  7. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

  8. LeetCode – Lemonade Change

    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...

  9. LeetCode.860-卖柠檬水找零(Lemonade Change)

    这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...

  10. C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4036 访问. 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...

随机推荐

  1. Springboot学习笔记(三)-常用注入组件方式

    包扫描@ComponentScan+组件标注注解(@Controller.@Service.@Repository.@Component) 包扫描不是必须的,指定包名后以指定的包名为准,比如指定包名为 ...

  2. [Full-stack] 快速上手开发 - React

    故事背景 [1] 博客笔记结合<React快速上手开发>再次系统地.全面地走一遍. [2] React JS Tutorials:包含了JS --> React --> Red ...

  3. [React] 09 - Tutorial: components

    jsx变为js的过程:http://babeljs.io/repl/ youtube: https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg ...

  4. 15适配器模式Adapter

    一.什么是适配器模式 Adapter模式也叫适配器模式,是构造型模式之一 ,通过Adapter模式可以改变已有类(或外部类)的接 口形式. 二.适配器模式应用场景 在大规模的系统开发过程中,我们常常碰 ...

  5. JavaScript隐藏的坑一,隐式调用toString

    最近在重新学习JavaScript,看动态原型对象的时候,打印了两个用同一个构造函数生成的对象,但是打印结果却不一样,请看代码: var box1=new Box(); console.log(box ...

  6. Android Studio开发第二篇创建新项目

    创建新项目很简单,File-New-New Project,这个没什么好说的跟Eclipse都差不都. 第二步SDK选择,有手机平板还有Wear,TV,汽车Auto,谷歌眼镜等几个种平台,这里就先选择 ...

  7. Excel 函数集(使用过的)

    1.   SUBTOTAL函数  筛选结果求和 SUBTOTAL(函数编号, 区域) 函数编号 为 1 到 11(包含隐藏值)或 101 到 111(忽略隐藏值)之间的数字,指定使用何种函数在数据清单 ...

  8. Qt编写调试日志输出类带网络转发(开源)

    用qt开发商业程序已经九年了,陆陆续续开发过至少几十个程序,除了一些算不算项目的小工具外,大部分的程序都需要有个日志的输出功能,希望可以将程序的运行状态存储到文本文件或者数据库或者做其他处理等,qt对 ...

  9. 使用 JdbcTemplate 查询数据时报错:列名无效(已解决)

    又犯了一个错误. 争取没有下次了. 就算再犯,也要知道去哪找答案. 所以,记录一下,以示警戒. 报错 使用 JdbcTemplate 查询数据时,出现异常: PreparedStatementCall ...

  10. C#设计模式--观察者模式(发布-订阅模式)

    0.C#设计模式--简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 6.C ...