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. 自定义元素 v1:可重用网络组件

    google文档 https://developers.google.cn/web/fundamentals/web-components/customelements 兼容性 https://can ...

  2. 壁虎书5 Support Vector Machine

    SVM is capable of performing linear or nonlinear classification,regression,and even outlier detectio ...

  3. [No0000F0]DataGrid一行Row添加ToolTip,wpf

    1. <Window x:Class="WpfApp7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  4. layer开启与关闭加载层

    // 开启加载层 layer.load(2, { shade: [0.6, '#fff'], content: '数据加载中...', success: function (layero) { lay ...

  5. 新浪广告交易平台(SAX)DSP手册

    新浪广告交易平台(SAX)DSP手册 http://amp.ad.sina.com.cn/sax/doc/zh-CN/xhtml/index.xhtml 新浪广告交易平台(SAX)DSP手册 版权 © ...

  6. linux 之awk

    简介 awk是一个强大的文本分析工具,相对grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格 为默认分隔符将每行切片,切开的部分再 ...

  7. kubenets installation--ranchor-mesos

    [kube-proxy]http://www.cnblogs.com/xuxinkun/p/5799986.html [flannel] 安装Flannel [root@master ~]# cd ~ ...

  8. [administrative][archlinux][clonezilla][disk cloning] 一块 windows 10 硬盘的备份

    https://wiki.archlinux.org/index.php/disk_cloning https://wiki.archlinux.org/index.php/full_system_b ...

  9. AndroidStudio_TextView

    写APP的工具为:AndroidStudio 主要在两种文件里编辑代码:.xml和.java(.xml里主要是调整布局相当于APP的前端 .java主要是写Java程序相当于APP的后端) 代码的调试 ...

  10. Fiddler笔记一移动端连接

    一.下载Fiddler 百度搜索”fiddler 下载“ ,安装最新版本   二.Fiddler手机抓包原理 在本机开启了一个http的代理服务器,然后它会转发所有的http请求和响应. Fiddle ...