[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 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 either5
,10
, or20
.class Solution(object):
def lemonadeChange(self, bills):
"""
:type bills: List[int]
:rtype: bool
"""
five=0
ten=0
for b in bills:
if b==10:
if five==0:
return False
five-=1
ten+=1
elif b==20:
if five>0 and ten>0:
five-=1
ten-=1
elif ten==0:
if five>=3:
five-=3
else:
return False
else:
return False
else:
five+=1 return True
[LeetCode&Python] Problem 860. Lemonade Change的更多相关文章
- [LeetCode&Python] Problem 860. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- 【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 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [LeetCode&Python] Problem 427. Construct Quad Tree
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...
- [LeetCode&Python] Problem 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- python操作文件(增、删、改、查)
内容 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode ...
- Ubuntu 系统优化(不定时更新)
系统美化 1. 为Ubuntu安装Numix主题和图标 sudo add-apt-repository ppa:numix/ppa sudo apt-get update sudo apt-get i ...
- 手机验证码JQUERY实现
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...
- 如何让一个div水平和垂直居中对齐
以下方法来自百度知道:https://zhidao.baidu.com/question/558984366971173044.html 方法1: .parent { width: 800px; he ...
- kiss prefix paleo,per,pen,pan,para out 1
1● paleo 2● per 3● pen 4● pan 5● para 1★ paleo 古 2★ para ,辅助,在旁边 3★ pan 广泛的 4★ per 假,坏,自始自 ...
- Win10系列:UWP界面布局基础5
(2)编写后台代码访问资源 下面通过一个例子来演示如何编写后台代码引用资源.新建一个Windows应用商店的空白应用程序项目,将其命名为AccessResourceApplication,打开项目下的 ...
- Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 每天CSS学习之text-indent
text-indent是CSS的一个属性,其作用是定义首行文本的缩进.其值如下: 1.length:首行缩进固定的长度.默认值为0. 设置 首行缩进2em的长度. div{ width:300px; ...
- 深入理解java虚拟机---虚拟机工具jmap(十六)
原文: https://www.cnblogs.com/myna/p/7573843.html jmap JVM Memory Map命令用于生成heap dump文件,如果不使用这个命令,还可以使用 ...
- 【重大更新】DevExpress WinForms v18.2新版亮点(七)
买 DevExpress Universal Subscription 免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...