当前位置:网站首页>LCP 06. 拿硬币-遍历
LCP 06. 拿硬币-遍历
2022-08-04 13:49:00 【Mr Gao】
LCP 06. 拿硬币
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
示例 1:
输入:[4,2,1]
输出:4
解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。
示例 2:
输入:[2,3,10]
输出:8
限制:
1 <= n <= 4
1 <= coins[i] <= 10
题目中虽然给了 n 堆硬币,但是最终每一堆都是要拿完的。而每一堆拿的情况又不影响其他硬币堆,因此每一堆硬币的拿法实际上是互相独立的。
于是我们可以只考虑一堆的情况。假设一堆有 x 枚硬币,既然我们的目的是尽早拿完所有硬币堆,那么两枚两枚的拿显然是更快的。
求单堆硬币最小次数:(x+1)//2
那么,拿完所有硬币堆只需要循环对所有硬币堆都计算一次,然后求和就可以了。
这题就很简单了,解题代码如下:
int minCount(int* coins, int coinsSize){
int sum=0;
for(int i=0;i<coinsSize;i++){
sum=sum+(coins[i]+1)/2;
}
return sum;
}
边栏推荐
猜你喜欢

Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置

This article sorts out the development of the main models of NLP

leetcode 48. Rotate Image 旋转图像(Medium)

Is there a replacement for the LM2596?LM2576 can

橄榄枝大课堂APP正式启动上线

How to play the Tower of Hanoi

LM2596有没有可以替代的?LM2576可以
![[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)](/img/9f/33e782b93fcaa15359450e59a7233d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)

"C pitfalls and pitfalls" reading summary

State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security
随机推荐
leetcode 48. Rotate Image (Medium)
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看
橄榄枝大课堂APP正式启动上线
C# winforms 输入颜色转换颜色名
router---路由守卫
座舱人机交互「暗潮汹涌」,语音「下」,多模态「上」
企业应当实施的5个云安全管理策略
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
VBS函数应用–getobject的使用获得Automation对象
Various problems with npm install
Button control switch 4017 digital circuit chip
搭建ros交叉编译环境(从x86到nvidia arm)
内存定位利器-ASAN使用小结
信创是什么意思?涉及哪些行业?为什么要发展信创?
工具函数---字符串处理
Utility function---string processing
并发刺客(False Sharing)——并发程序的隐藏杀手
项目里的各种配置,你都了解吗?
正确使用Impala的invalidate metadata与refresh语句
LeetCode_424_替换后的最长重复字符