问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

在柠檬水摊上,每一杯柠檬水的售价为 5 美元。

顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。

每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。

注意,一开始你手头没有任何零钱。

如果你能给每位顾客正确找零,返回 true ,否则返回 false 。

输入:[5,5,5,10,20]

输出:true

解释:前 3 位顾客那里,我们按顺序收取 3 张 5 美元的钞票。第 4 位顾客那里,我们收取一张 10 美元的钞票,并返还 5 美元。第 5 位顾客那里,我们找还一张 10 美元的钞票和一张 5 美元的钞票。由于所有客户都得到了正确的找零,所以我们输出 true。

输入:[5,5,10]

输出:true

输入:[10,10]

输出:false

输入:[5,5,10,10,20]

输出:false

解释:前 2 位顾客那里,我们按顺序收取 2 张 5 美元的钞票。对于接下来的 2 位顾客,我们收取一张 10 美元的钞票,然后返还 5 美元。对于最后一位顾客,我们无法退回 15 美元,因为我们现在只有两张 10 美元的钞票。由于不是每位顾客都得到了正确的找零,所以答案是 false。

提示:

  • 0 <= bills.length <= 10000
  • bills[i] 不是 5 就是 10 或是 20

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.

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.

Input: [5,5,10]

Output: true

Input: [10,10]

Output: false

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 <= 10000
  • bills[i] will be either 5, 10, or 20.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

public class Program {

    public static void Main(string[] args) {
var bills = new int[] { 5, 5, 5, 10, 20 }; var res = LemonadeChange(bills);
Console.WriteLine(res); bills = new int[] { 5, 5, 10, 10, 20 }; res = LemonadeChange2(bills);
Console.WriteLine(res); bills = new int[] { 5, 10 }; res = LemonadeChange3(bills);
Console.WriteLine(res); Console.ReadKey();
} public static bool LemonadeChange(int[] bills) {
//统计5美元和10美元的数量
var five = 0;
var ten = 0;
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
//5美元,则5美元+1
five++;
} else if(bills[i] == 10) {
//10美元,则10美元+1,找零5美元,5美元-1
ten++;
five--;
} else if(bills[i] == 20) {
//20美元时,尽量先找零10美元和5美元
//然后考虑找零3个5美元
if(ten > 0) {
ten--;
five--;
} else {
five -= 3;
}
}
//5美元或10美元小于0时,说明至少有1次找零是失败的
//直接返回 false
if(five < 0 || ten < 0) return false;
}
//最后返回 true
return true;
} public static bool LemonadeChange2(int[] bills) {
//基本思路同 LemonadeChange,提供了一个不同的实现
var list = new List<int>();
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
list.Add(5);
} else if(bills[i] == 10) {
list.Add(10);
if(!list.Remove(5)) return false;
} else if(bills[i] == 20) {
if(list.Remove(10)) {
if(!list.Remove(5)) return false;
} else {
for(var j = 0; j < 3; j++) {
if(!list.Remove(5)) return false;
}
}
}
}
return true;
} public static bool LemonadeChange3(int[] bills) {
//不建议的解法,仅提供思路
//基本思路同 LemonadeChange,提供了一个不同的实现
try {
var five = new Stack<int>();
var ten = new Stack<int>();
for(var i = 0; i < bills.Length; i++) {
if(bills[i] == 5) {
five.Push(5);//这个值是什么无所谓
} else if(bills[i] == 10) {
ten.Push(10);//这个值是什么无所谓
five.Pop();
} else if(bills[i] == 20) {
if(ten.Any()) {
ten.Pop();
five.Pop();
} else {
for(var j = 0; j < 3; j++) {
five.Pop();
}
}
}
}
return true;
} catch(Exception ex) {
return false;
}
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4036 访问。

True
False
True

分析:

显而易见,以上3种算法的时间复杂度均为:  。

C#LeetCode刷题之#860-柠檬水找零(Lemonade Change)的更多相关文章

  1. [Swift]LeetCode860. 柠檬水找零 | Lemonade Change

    At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...

  2. Leetcode 860. 柠檬水找零

    860. 柠檬水找零  显示英文描述 我的提交返回竞赛   用户通过次数187 用户尝试次数211 通过次数195 提交次数437 题目难度Easy 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾 ...

  3. 【LeetCode】860. 柠檬水找零

    860. 柠檬水找零 知识点:贪心 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 ...

  4. LeetCode 860.柠檬水找零(C++)

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

  5. LeetCode 860. 柠檬水找零 (贪心)

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

  6. [LeetCode] 860. 柠檬水找零 lemonade-change(贪心算法)

    思路: 收到5块时,只是添加:收到十块时,添加10块,删除一个5块:收到20块时,添加20,删除一个10块一个5块,或者直接删除3个5块(注意:这里先删除5+10优于3个5) class Soluti ...

  7. LeetCode:柠檬水找零【860】

    LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...

  8. LeetCode.860-卖柠檬水找零(Lemonade Change)

    这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...

  9. Leetcode860.Lemonade Change柠檬水找零

    在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...

随机推荐

  1. redis linux开机启动 (简单高效)

    1. 在edis下载文件包中找 redis/utils 找到redis_init_script 将它拷贝到  /etc/init.d 目录并重命名为redis cd redis cd utils mv ...

  2. SpringBoot系列之Elasticsearch极速入门与实际教程

    @ 目录 一.什么Elasticsearch? 二.Elasticsearch安装部署 2.1 Elasticsearch安装环境准备 2.2 Docker环境安装Elasticsearch 2.3 ...

  3. 第二章: IPC机制

    这章关于进程的概念,没有深入太多,只做了解跟学习 IPC: Inter-Process Communication,进程间通信或者跨进程通信,两个进程之间进行数据交换的过程 2.1介绍 线程:CPU调 ...

  4. OSCP Learning Notes - WebApp Exploitation(5)

    Remote File Inclusion[RFI] Prepare: Download the DVWA from the following website and deploy it on yo ...

  5. Oracle RMAN 异机恢复一例

    背景介绍:本例需求是将NBU备份的oracle数据库恢复到另一主机上. NBU环境配置.异机上的Oracle软件安装配置忽略,下面只介绍OracleDB恢复的过程. ----------------- ...

  6. CENTOS下搭建git代码仓库 ssh协议

    centos服务器下搭建git仓库,使用ssh协议管理仓库代码权限    git官网(http://git-scm.com/) 使用ssh协议: 一.安装git,使用yum install git 或 ...

  7. JQuery对下拉列表Select的一些操作

    1.假如select中存在选项,需要清空的情况: $("#search").find("option").remove(); $("#search&q ...

  8. 盘点 6 个被淘汰的 Java 技术,它们都曾经风光过!

    大家好啊,今天栈长给大家分享下我的开发历程中,我知道的那些被淘汰的技术或者框架,有些我甚至都没有用过,但我知道它曾经风光过. 废话不多说,下面我要开始吹了-- 1.Swing 下面这个是用 swing ...

  9. Java中使用断言

    由于断言在Java程序中用于开发和测试阶段,考虑到以后很有可能会用到,在此先记类一下. 在Java语言中,给出了3种处理系统错误的机制: 1.抛出一个异常 2.日志 3.使用断言 什么时候使用断言呢? ...

  10. Java基础之常用知识点博客汇总

    正则: 正则表达式 :https://www.cnblogs.com/lzq198754/p/5780340.html 正则表达式大全:https://blog.csdn.net/zpz2411232 ...