LeetCode LCP 06. 拿硬币
题目描述
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
示例 1:
输入:[4,2,1]
输出:4
解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完
一、解题关键词
数组。贪心。遍历 。奇数偶数讨论
二、解题报告
1.思路分析
1、数组遍历
2.时间复杂度
3.代码示例
tips:
class Solution {
public int minCount(int[] coins) {
int len = coins.length;
int result = 0;
for(int i = 0; i < len; i ++){
int lenCoin = coins[i];
if(lenCoin % 2 == 0){
result += coins[i] / 2;
}else {
result += coins[i] / 2 + 1;
}
}
return result;
}
}
2.知识点