当前位置:网站首页>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;
}
边栏推荐
- Auto. Getting started with JS
- TCP's three handshakes and four waves
- STM32 how to use stlink download program: light LED running light (Library version)
- 树莓派4B安装opencv3.4.0
- Determine the Photo Position
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- nodejs爬虫
- 【练习4-1】Cake Distribution(分配蛋糕)
- 基于web的照片数码冲印网站
- 栈的经典应用—括号匹配问题
猜你喜欢
树莓派4B安装opencv3.4.0
Penetration test (7) -- vulnerability scanning tool Nessus
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
信息安全-安全编排自动化与响应 (SOAR) 技术解析
渗透测试 ( 1 ) --- 必备 工具、导航
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Borg Maze (BFS+最小生成树)(解题报告)
渗透测试 ( 4 ) --- Meterpreter 命令详解
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Pyside6 signal, slot
随机推荐
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
nodejs爬虫
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
The most complete programming language online API document
JS call camera
Opencv learning log 33 Gaussian mean filtering
[exercise-5] (UVA 839) not so mobile (balance)
【练习-11】4 Values whose Sum is 0(和为0的4个值)
Opencv learning log 27 -- chip positioning
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
7-1 懂的都懂 (20 分)
Information security - security professional name | CVE | rce | POC | Vul | 0day
Gartner: five suggestions on best practices for zero trust network access
【练习-5】(Uva 839)Not so Mobile(天平)
Information security - threat detection engine - common rule engine base performance comparison
X-Forwarded-For详解、如何获取到客户端IP
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Nodejs+vue网上鲜花店销售信息系统express+mysql
CEP used by Flink
The concept of C language array