当前位置:网站首页>leetcode-1833. 雪糕的最大数量(排序+贪心)
leetcode-1833. 雪糕的最大数量(排序+贪心)
2022-07-31 05:10:00 【lin钟一】

题目链接:https://leetcode.cn/problems/maximum-ice-cream-bars/
思路
直观想法
在给定的硬币情况下,花最小的钱,买最多的雪糕,一眼贪心。
吐槽一句:这题mid难度有点离谱,easy题差不多,经典贪心题
算法
- 对原雪糕价格 costs 数组进行小到大排序
- 遍历 costs 数组,当前的雪糕价格不超过硬币数,则购买,直接减去当前雪糕价格,不用关心怎么搭配买最多,只管当前他最便宜我就买,贪心!
代码示例
func maxIceCream(costs []int, coins int) (ans int) {
//go自带的排序x
sort.Ints(costs)
for i := range costs{
if coins - costs[i] < 0{
break
}
coins -= costs[i]
ans++
}
return
}

复杂度分析
- 时间复杂度:O(n logn) 其中n 是数组 costs 的长度,对数组排序所需要的时间是O(n logn),遍历数组需要O(n)的时间,以上时间取最长则是O(n logn)
- 空间复杂度:O(logn),其中 nn 是数组 costs 的长度。空间复杂度主要取决于排序使用的额外空间。
边栏推荐
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 详解扫雷游戏(C语言)
- gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
- Why use Flink and how to get started with Flink?
- 三子棋讲解(C语言)
- Quickly master concurrent programming --- the basics
- If the account number or password is entered incorrectly for many times, the account will be banned.
- tf.keras.utils.get_file()
- 【C语言3个基本结构详解——顺序、选择、循环】
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
猜你喜欢

Anaconda configure environment directives

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it

The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式

剑指offer基础版 ----- 第25天

08 【生命周期 组件】

MySQL(更新中)

The interviewer asked me TCP three handshake and four wave, I really

剑指offer专项突击版 ---- 第2天
随机推荐
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
02 【el和data的两种写法 MVVM模型】
C语言指针详解
Why use Flink and how to get started with Flink?
面试官,不要再问我三次握手和四次挥手
Element concatenation operations in numpy and pytorch: stack, concatenat, cat
剑指offer专项突击版 --- 第 3 天
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
关于LocalDateTime的全局返回时间带“T“的时间格式处理
Refinement of the four major collection frameworks: Summary of List core knowledge
07 【内置指令 自定义指令】
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
【LeetCode-SQL每日一练】——2. 第二高的薪水
Lock wait timeout exceeded解决方案
08 【生命周期 组件】
解决响应式数据依赖响应式数据无响应问题
面试官问我TCP三次握手和四次挥手,我真的是
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
剑指offer基础版 ----- 第25天
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)