LeetCode LCP 06. 拿硬币

    技术2026-02-26  9

    目录结构

    1.题目

    2.题解


    1.题目

    桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。

    示例:

    输入:[4,2,1] 输出:4 解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。 输入:[2,3,10] 输出:8

    限制:

    1 <= n <= 41 <= coins[i] <= 10

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/na-ying-bi 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    2.题解

    遍历。

    每一堆硬币先两个两个拿,若只剩一个时则只拿一个。

    public class SolutionLCP06 { public int minCount(int[] coins) { int result = 0; for (int coin : coins) { result += coin / 2 + coin % 2; } return result; } } 时间复杂度:空间复杂度:
    Processed: 0.010, SQL: 9