桌上有
n堆力扣币,每堆的数量保存在数组coins中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
思路
拿完某堆硬币需要的最少次数为 c e i l ( c o i n / 2 ) ceil(coin/2) ceil(coin/2),累加求和返回最终结果
实现
class Solution {
public int minCount(int[] coins) {
int res = 0;
for (int coin : coins){
res += coin / 2 + coin % 2;
}
return res;
}
}