当前位置:网站首页>860. Lemonade change
860. Lemonade change
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/lemonade-change/
subject :
On the lemonade stand , The price of each lemonade is 5 dollar . Customers line up to buy your products ,( By bill bills The order of payment ) Buy one cup at a time .
Each customer only buys a glass of lemonade , Then pay you 5 dollar 、10 US dollars or 20 dollar . You have to give every customer the right change , That is to say, the net transaction is that every customer pays you 5 dollar .
Be careful , You didn't have any change at first .
Give you an array of integers bills , among bills[i] It's No i A customer paid the bill . If you can give every customer the right change , return true , Otherwise return to false .
Example 1:
| Input :bills = [5,5,5,10,20] Output :true explain : front 3 There are customers , We charge in order 3 Zhang 5 Dollar bills . The first 4 There are customers , We take one 10 Dollar bills , And return it 5 dollar . The first 5 There are customers , Let's return one 10 A dollar bill and a 5 Dollar bills . Because all customers get the right change , So we output true. |
Example 2:
| Input :bills = [5,5,10,10,20] Output :false explain : front 2 There are customers , We charge in order 2 Zhang 5 Dollar bills . For the next 2 Customers , We take one 10 Dollar bills , And return it 5 dollar . For the last customer , We can't return 15 dollar , Because we only have two 10 Dollar bills . Because not every customer gets the right change , So the answer is false. |
Example 3:
| Input :bills = [5,5,10] Output :true |
Example 4:
| Input :bills = [10,10] Output :false |
Tips :
| 1 <= bills.length <= 105 bills[i] No 5 Namely 10 or 20 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/lemonade-change
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
The cashier's idea is ok , Cash register yes 3 Lattice , Separately put 5,10,20
No money at work , The lattice is empty , Normal business is ok , No advance payment
Method 1 、 Count array
It is not strictly a counting array operation , It's similar
bool lemonadeChange(int* bills, int billsSize){
int count[3] = {0};
for(int i=0; i<billsSize; i++)
{
if(bills[i] == 5)
count[0]++;
else if(bills[i] == 10)
{
if(count[0] != 0)
{
count[0]--;
count[1]++;
}
else
return false;
}
else if(bills[i] == 20)
{
if(count[1] != 0)
{
count[1]--;
if(count[0] != 0)
{
count[0]--;
count[2]++;
}
else
return false;
}
else
{
if(count[0] >= 3)
{
count[0] -= 3;
count[2]++;
}
else
return false;
}
}
}
return true;
}边栏推荐
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- Opencv learning log 12 binarization of Otsu method
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- Penetration test (4) -- detailed explanation of meterpreter command
- MySQL grants the user the operation permission of the specified content
- Opencv learning log 18 Canny operator
- Penetration test (3) -- Metasploit framework (MSF)
- Path problem before dynamic planning
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
- Common configuration files of SSM framework
猜你喜欢

Luogu P1102 A-B number pair (dichotomy, map, double pointer)

Nodejs+vue online fresh flower shop sales information system express+mysql

信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
快速转 TypeScript 指南

树莓派4B安装opencv3.4.0
frida hook so层、protobuf 数据解析

Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability

渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具

滲透測試 ( 1 ) --- 必備 工具、導航

Web based photo digital printing website
随机推荐
TCP's three handshakes and four waves
Penetration test (1) -- necessary tools, navigation
最全编程语言在线 API 文档
【练习-9】Zombie’s Treasure Chest
Research Report on market supply and demand and strategy of China's earth drilling industry
Penetration test (7) -- vulnerability scanning tool Nessus
China's peripheral catheter market trend report, technological innovation and market forecast
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
CS zero foundation introductory learning record
[exercise -10] unread messages
Penetration test (3) -- Metasploit framework (MSF)
Truck History
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Opencv learning log 13 corrosion, expansion, opening and closing operations
MySQL grants the user the operation permission of the specified content
China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
[exercise-6] (UVA 725) division = = violence
Nodejs+vue online fresh flower shop sales information system express+mysql
Common configuration files of SSM framework
Truck History