当前位置:网站首页>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;
}边栏推荐
- 初入Redis
- Penetration test (3) -- Metasploit framework (MSF)
- CS zero foundation introductory learning record
- socket通讯
- STM32 learning record: LED light flashes (register version)
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- 7-1 understand everything (20 points)
- 树莓派4B安装opencv3.4.0
- Interesting drink
猜你喜欢

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

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

Information security - threat detection engine - common rule engine base performance comparison

Penetration test (4) -- detailed explanation of meterpreter command

Penetration test (1) -- necessary tools, navigation

渗透测试 ( 3 ) --- Metasploit Framework ( MSF )

Data storage in memory & loading into memory to make the program run

2078. Two houses with different colors and the farthest distance

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

1323. Maximum number of 6 and 9
随机推荐
frida hook so层、protobuf 数据解析
D - Function(HDU - 6546)女生赛
E. Breaking the Wall
JS call camera
Matlab comprehensive exercise: application in signal and system
D - function (HDU - 6546) girls' competition
HDU-6025-Coprime Sequence(女生赛)
Shell脚本编程
Nodejs+vue online fresh flower shop sales information system express+mysql
Opencv learning log 18 Canny operator
Flink 使用之 CEP
Frida hook so layer, protobuf data analysis
If you want to apply for a programmer, your resume should be written like this [essence summary]
1010 things that college students majoring in it must do before graduation
The most complete programming language online API document
Opencv learning log 13 corrosion, expansion, opening and closing operations
基于web的照片数码冲印网站
Hdu-6025-prime sequence (girls' competition)
nodejs爬虫
CS zero foundation introductory learning record