返回介绍

solution / 3000-3099 / 3074.Apple Redistribution into Boxes / README_EN

发布于 2024-06-17 01:02:57 字数 4165 浏览 0 评论 0 收藏 0

3074. Apple Redistribution into Boxes

中文文档

Description

You are given an array apple of size n and an array capacity of size m.

There are n packs where the ith pack contains apple[i] apples. There are m boxes as well, and the ith box has a capacity of capacity[i] apples.

Return _the minimum number of boxes you need to select to redistribute these _n_ packs of apples into boxes_.

Note that, apples from the same pack can be distributed into different boxes.

 

Example 1:

Input: apple = [1,3,2], capacity = [4,3,1,5,2]
Output: 2
Explanation: We will use boxes with capacities 4 and 5.
It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.

Example 2:

Input: apple = [5,5,5], capacity = [2,4,2,7]
Output: 4
Explanation: We will need to use all the boxes.

 

Constraints:

  • 1 <= n == apple.length <= 50
  • 1 <= m == capacity.length <= 50
  • 1 <= apple[i], capacity[i] <= 50
  • The input is generated such that it's possible to redistribute packs of apples into boxes.

Solutions

Solution 1: Greedy + Sorting

To minimize the number of boxes needed, we should prioritize using boxes with larger capacities. Therefore, we can sort the boxes in descending order of capacity, and then use the boxes one by one until all the apples are packed. We return the number of boxes used at this point.

The time complexity is $O(m \times \log m + n)$ and the space complexity is $O(\log m)$, where $m$ and $n$ are the lengths of the arrays capacity and apple respectively.

class Solution:
  def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int:
    capacity.sort(reverse=True)
    s = sum(apple)
    for i, c in enumerate(capacity, 1):
      s -= c
      if s <= 0:
        return i
class Solution {
  public int minimumBoxes(int[] apple, int[] capacity) {
    Arrays.sort(capacity);
    int s = 0;
    for (int x : apple) {
      s += x;
    }
    for (int i = 1, n = capacity.length;; ++i) {
      s -= capacity[n - i];
      if (s <= 0) {
        return i;
      }
    }
  }
}
class Solution {
public:
  int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
    sort(capacity.rbegin(), capacity.rend());
    int s = accumulate(apple.begin(), apple.end(), 0);
    for (int i = 1;; ++i) {
      s -= capacity[i - 1];
      if (s <= 0) {
        return i;
      }
    }
  }
};
func minimumBoxes(apple []int, capacity []int) int {
  sort.Ints(capacity)
  s := 0
  for _, x := range apple {
    s += x
  }
  for i := 1; ; i++ {
    s -= capacity[len(capacity)-i]
    if s <= 0 {
      return i
    }
  }
}
function minimumBoxes(apple: number[], capacity: number[]): number {
  capacity.sort((a, b) => b - a);
  let s = apple.reduce((acc, cur) => acc + cur, 0);
  for (let i = 1; ; ++i) {
    s -= capacity[i - 1];
    if (s <= 0) {
      return i;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文