/**
* 桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
* <p>
* 示例 1:
* <p>
* 输入:[4,2,1]
* <p>
* 输出:4
* <p>
* 解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。
* <p>
* 示例 2:
* <p>
* 输入:[2,3,10]
* <p>
* 输出:8
* <p>
* 限制:
* <p>
* 1 <= n <= 4
* 1 <= coins[i] <= 10
*/
class Solution {
public int minCount(int[] coins) {
int ans = 0;
for (int i = 0; i < coins.length; i++) {
if (coins[i] % 2 != 0) {
ans += (coins[i] - 1) / 2 + 1;
} else {
ans += coins[i] / 2;
}
}
return ans;
}
public static void main(String[] args) {
int[] coins = new int[]{4, 2, 1};
System.out.println(new Solution().minCount(coins));
}
}
LCP 06. 拿硬币
最新推荐文章于 2024-05-21 19:48:43 发布