当前位置:网站首页>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 的长度。空间复杂度主要取决于排序使用的额外空间。
边栏推荐
- “档次法”——用于物品体积分布不均匀的01背包问题的求解方法
- 基于flask的三方登陆的流程
- mysql5.7.35安装配置教程【超级详细安装教程】
- Kubernetes certificate validity period modification
- 13 【代理配置 插槽】
- leetcode-每日一题558. 四叉树交集(分治递归)
- uni-app进阶之创建组件/原生渲染【day9】
- If the account number or password is entered incorrectly for many times, the account will be banned.
- 剑指offer专项突击版 --- 第 4 天
- C语言实验一 熟悉C程序的环境
猜你喜欢
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
uni-app进阶之认证【day12】
Proteus 8 Professional安装教程
【C语言3个基本结构详解——顺序、选择、循环】
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Sword Point Offer Special Assault Edition ---- Day 1
C语言实验三 选择结构程序设计
为什么要用Flink,怎么入门使用Flink?
leetcode-每日一题731. 我的日程安排表 II
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
随机推荐
解决响应式数据依赖响应式数据无响应问题
11 【定位】
Flask-based three-party login process
Anaconda配置环境指令
第7章 网络层第3次练习题答案(第三版)
数据库学习笔记
C语言实验五 循环结构程序设计(二)
C语言实验四 循环结构程序设计(一)
Input length must be multiple of 8 when decrypting with padded cipher
08 【生命周期 组件】
10 【高度塌陷与BFC】
The process and specific code of sending SMS verification code using flask framework
torch.normal function usage
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Flask 的初识
联盟链的真实场景在哪里
快速掌握并发编程 --- 基础篇
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
【MQ我可以讲一个小时】
剑指offer专项突击版 ---第 5 天