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. B - Calculation 2

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  2. 我所知道的几种display:table-cell的应用

    .outer span {  display: table-cell; } 一.display:table-cell属性简述 display:table-cell属性指让标签元素以表格单元格的形式呈现 ...

  3. C语言复制图片文件

    以下代码将文件一的图片复制到文件二中 #include<stdio.h> #include<stdlib.h> int main() { char ch; char fname ...

  4. H - Gold Coins(2.4.1)

    H - Gold Coins(2.4.1) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:3000 ...

  5. matplotlib --> r`$...$`

    文档中介绍的很详细:https://matplotlib.org/tutorials/text/mathtext.html matplotlib  Tutoials --> Text --> ...

  6. nvidia-smi failed because it couldn't communicate with the nvidia driver

    Ubuntu装好CUDA之后过段时间提示NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. NV ...

  7. bilibili的直播第三方IJKMediaFramework.framework下载打包使用教程

    参考和引用的地址: http://www.code4app.com/thread-8941-1-1.html http://blog.csdn.net/cccallen/article/details ...

  8. sql 一对多查询最近一条

    感谢 http://bbs.csdn.net/topics/391048578?page=1 create table A ( [Id] [uniqueidentifier] NOT NULL, ) ...

  9. 2018/05/11 PHP 设计模式之 适配器模式

    什么是适配器模式? 简单来说,我想买一根充电线,我买一根安卓的?还是买一根苹果的? 我也不确定,因为我以可能会换手机,对于我的形式我也不确定. 所以,我要买一根可以同时适配 安卓/苹果 的线. 所谓适 ...

  10. BOM简单总结

    先来说一下BOM,什么是BOM?BOM就是浏览器对象模型,大家都知道,ECMAScript是JavaScript的核心,但如果要在WEB中使用JavaScript,那么BOM则无疑才是真正的核心,BO ...