当前位置:网站首页>860. 柠檬水找零
860. 柠檬水找零
2022-06-11 07:43:00 【zzu菜】
860. 柠檬水找零
在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。
每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。
注意,一开始你手头没有任何零钱。
给你一个整数数组 bills ,其中 bills[i] 是第 i 位顾客付的账。如果你能给每位顾客正确找零,返回 true ,否则返回 false 。
示例 1:
输入:bills = [5,5,5,10,20]
输出:true
解释:
前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。
第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。
第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。
由于所有客户都得到了正确的找零,所以我们输出 true。
示例 2:
输入:bills = [5,5,10,10,20]
输出:false
解释:
前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。
对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。
对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。
由于不是每位顾客都得到了正确的找零,所以答案是 false。
思考
题外话:
说真的现如今这题已经不合适了,谁出门还带现金,难道不是支付宝微信嘛?
题解:
设置三个变量fiveRMB、tenRMB、twentyRMB分别代表其5美元10美元15美元人民币的个数
首先判断第一张货币是不是5美元,因为此时我们没有零钱。
所以初始化 fiveRMB=1、tenRMB=0、twentyRMB=0
接下来去遍历每个顾客给的零钱。
- 5块钱,不用找零,fiveRMB++
- 10块钱,找零5快,fiveRMB- - ,tenRMB++
- 20块钱,找零分两种情况,我们这里优先消耗10元
- 找一张10元,一张5元
- 找3张5元
class Solution {
public boolean lemonadeChange(int[] bills) {
if(bills[0]!=5) return false;
int fiveRMB=1;
int tenRMB=0;
int twentyRMB=0;
for (int i =1;i<bills.length;i++){
if(bills[i]==5) {
fiveRMB++;
continue;
}
if(bills[i]==10){
if(fiveRMB==0){
return false;
}
tenRMB++;
fiveRMB--;
continue;
}
if(bills[i]==20){
twentyRMB++;
if(fiveRMB>=1&&tenRMB>=1){
fiveRMB=fiveRMB-1;
tenRMB=tenRMB-1;
continue;
}
if(fiveRMB>=3){
fiveRMB=fiveRMB-3;
continue;
}
return false;
}
}
return true;
}
}
边栏推荐
- You got 8K in the 3-year function test, but you were actually pretending to work hard
- 20200810 T2 dispatch money
- 3年功能测试拿8K,被新来的反超,其实你在假装努力
- Long dialogue in June 2017
- Aiop introduction
- 【AtCoder1998】Stamp Rally(整体二分+并查集)
- [noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
- [poj3691] DNA repair (AC automata +dp)
- 2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
- .NET C#基础(6):命名空间 - 有名字的作用域
猜你喜欢

【软件测试】这样的简历已经刷掉了90%的面试者

Qunhui ds918 creates m.2 SSD read / write cache

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...

群晖DS918创建m.2 固态硬盘SSD读写缓存

How to output the percent sign "%" in printf function in C language
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...

How to prepare for the new PMP syllabus exam?

The solution of "no startup device" after running Bochs
随机推荐
C language function stack frame
Matrix tree theorem
[untitled] Weng_ C lesson 1
Remote office experience | community essay solicitation
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
【AtCoder2305】Decrementing(博弈)
QT table display data
[cluster] haproxy load balancing
Miscellany C language
如何准备PMP新版大纲考试?
May 30-June 5, 2022 AI industry weekly (issue 100): three years
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
The maximum number of divisors of numbers in the int range is 1536
20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
2021-11-05 definition of cache
C language to achieve minesweeping games
Introduction to operations research
multi-sig SC
Qunhui ds918 creates m.2 SSD read / write cache
Long dialogue in June 2017