[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 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 <= 10000bills[i]will be either5,10, or20.
零钱只有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的更多相关文章
- [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 ...
- [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 ...
- 【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 860. Lemonade Change
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [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 ...
- [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 ...
随机推荐
- perl 读取一个文件 替换文件的关键词 把数据替换到新的文件
replace # replace #!/usr/bin/perl my @data = (); my ($fname ,$rp, $nfname)= @ARGV; my ($o, $n) = spl ...
- Python2安装igraph
前言 igraph是一个进行图计算和社交网络分析的软件包,支持python语言,打算学习igraph,然后应用在自己的项目中. 系统环境 64位win10系统,同时安装了python3.6和pytho ...
- Android超精准计步器开发-Dylan计步(申明:来源于网路)
Android超精准计步器开发-Dylan计步(申明:来源于网路) 拿来借鉴学习,向原创者... 地址:http://blog.csdn.net/linglongxin24/article/detai ...
- nginx js、css、图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常
nginx js.css.图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常 http://upstreamname:port/....../. ...
- SpringMVC中@RestController的用法
转自:https://blog.csdn.net/u010412719/article/details/69710480 Spring4之后新加入的注解,原来返回json需要@ResponseBody ...
- spark分组统计及二次排序案例一枚
组织数据形式: aa 11 bb 11 cc 34 aa 22 bb 67 cc 29 aa 36 bb 33 cc 30 aa 42 bb 44 cc 49 需求: 1.对上述数据按key值进行分组 ...
- 小心踩雷,一次Java内存泄漏排查实战
1.使用 jstack pid > jstack.log 保存了线程栈的现场,使用 jmap -dump:format=b,file=heap.log pid 保存了堆现场: https://m ...
- NULL - AUTO_INCREMENT
http://dev.mysql.com/doc/refman/5.7/en/create-table.html Data Types and Attributes for Columns data_ ...
- iOS 动画学习之视图控制器转场动画
一.概述 1.系统会创建一个转场相关的上下文对象,传递到动画执行器的animateTransition:和transitionDuration:方法,同样,也会传递到交互Controller的star ...
- [dpdk] dpdk多线程任务调度
DPDK下的线程,叫做EAL线程. EAL线程默认是与CPU core一对一绑定的,这样的话,有一些实时性,计算量不高的任务独占CORE是一种浪费,大概找了如下几种解决方案. 1. dpdk seri ...