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.

零钱只有5, 10, 而20 可以不用管. ans = [0]*2, 分别存5的张数和10 的张数

1. if 5, ans[0] += 1

2. if 10, ans[0] -= 1

3. if 20, first if ans[1] > 0 then , ans[0] -= 1, ans[1] -= 1

  else: ans[0] -= 3

然后看ans[0] <0, return False

else: return True

Code    T: O(n)   S; O(1)

class Solution(object):
def lemonadeChange(self, bills):
ans = [0,0] # present 5, 10
for money in bills:
if money == 5:
ans[0] += 1
elif money == 10:
ans[0] -= 1
ans[1] += 1
else:
if ans[1]:
ans[1] -= 1
ans[0] -= 1
else:
ans[0] -= 3
if ans[0] <0 : return False
return True

[LeetCode] 860. Lemonade Change_Easy tag: Greedy的更多相关文章

  1. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  2. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  3. 【Leetcode_easy】860. Lemonade Change

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

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

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

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

  6. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  7. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  8. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  9. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

随机推荐

  1. favicon.ico 网站小图标标识

    随便打开一个网页:比如 http://www.baidu.com/ 可以看到在浏览器的标签头上面显示了一个图标,这个图标是:,也就是我们常说的favicon.ico. 由于这篇文章主要讨论favico ...

  2. ThinkPHP框架 【 AJAX方法返回 】 例子:简单添加一条数据 和 查询一个表里的数据

    注:thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 在模块控制器Controller文 ...

  3. uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比

    框架列表. https://www.cnblogs.com/xiaxiaxia/articles/5705557.html 前言 近期,要开一个新的项目,APP类型.最重要的需求就是能够随时调整APP ...

  4. struts2常用标签详解(申明:来源于网络)

    struts2常用标签详解(申明:来源于网络) 地址:http://blessht.iteye.com/blog/1184960

  5. day10 十 函数、形参和实参

    一.形参和实参 1.形参:在函数定义()中出现的参数形参就是拷贝实参的值,随着函数的调用才产生,随着函数调用结束而销毁 def fn(a, b, c): print(a) print(b) print ...

  6. [No000010A]Git3/9-创建版本库

    什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...

  7. centos7安装Apache

    1.下载安装包wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.37.tar.gz 2.解压tar zxvf httpd-2.4.37.ta ...

  8. radio样式的写法,单选和多选如何快速的改变默认样式,纯CSS,

    一.纯CSS写法改变单选框的默认选择样式,用背景图片代替 input[type='radio']:radio:before { content: '';//这里需要有 width: 20px; hei ...

  9. 微博登录报错 sso package orsign error

    https://blog.csdn.net/gao_chun/article/details/41344725 (1)检查应用包名签名信息是否完善 如果你的应用只有一个包名.签名,请在 http:// ...

  10. 20165225《Java程序设计》第五周学习总结

    20165225<Java程序设计>第五周学习总结 1.视频与课本中的学习: - 第七章学习总结 内部类: 内部类的外嵌类的成员变量在内部类中仍然有效,内部类中的方法也可以调用外嵌类中的方 ...