重新分装苹果

重新分装苹果

#resource / algorithm #type / snippet #status / evergreen #source / leetcode #algo / greedy

[!info] related notes 算法面试题型 MOC 贪心算法

重新分装苹果

题目

3074. 重新分装苹果 - 力扣(LeetCode)

题解

贪心算法

var minimumBoxes = function (apple, capacity) {
    let res = 0;
    let sum = apple.reduce((acc, cur) =>
        acc + cur, 0
    )
    capacity = capacity.sort((a, b) => b - a)
    for (const cap of capacity) {
        sum -= cap;
        res++;
        if (sum <= 0) {
            return res;
        }
    }
    return res;
};

参考

创建于 2025/1/1 更新于 2026/5/27