|
| 1 | +# [3074. Apple Redistribution into Boxes](https://leetcode.com/problems/apple-redistribution-into-boxes) |
| 2 | + |
| 3 | +[中文文档](/solution/3000-3099/3074.Apple%20Redistribution%20into%20Boxes/README.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## Description |
| 8 | + |
| 9 | +<p>You are given an array <code>apple</code> of size <code>n</code> and an array <code>capacity</code> of size <code>m</code>.</p> |
| 10 | + |
| 11 | +<p>There are <code>n</code> packs where the <code>i<sup>th</sup></code> pack contains <code>apple[i]</code> apples. There are <code>m</code> boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of <code>capacity[i]</code> apples.</p> |
| 12 | + |
| 13 | +<p>Return <em>the <strong>minimum</strong> number of boxes you need to select to redistribute these </em><code>n</code><em> packs of apples into boxes</em>.</p> |
| 14 | + |
| 15 | +<p><strong>Note</strong> that, apples from the same pack can be distributed into different boxes.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong class="example">Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input:</strong> apple = [1,3,2], capacity = [4,3,1,5,2] |
| 22 | +<strong>Output:</strong> 2 |
| 23 | +<strong>Explanation:</strong> We will use boxes with capacities 4 and 5. |
| 24 | +It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> apple = [5,5,5], capacity = [2,4,2,7] |
| 31 | +<strong>Output:</strong> 4 |
| 32 | +<strong>Explanation:</strong> We will need to use all the boxes. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong>Constraints:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>1 <= n == apple.length <= 50</code></li> |
| 40 | + <li><code>1 <= m == capacity.length <= 50</code></li> |
| 41 | + <li><code>1 <= apple[i], capacity[i] <= 50</code></li> |
| 42 | + <li>The input is generated such that it's possible to redistribute packs of apples into boxes.</li> |
| 43 | +</ul> |
| 44 | + |
| 45 | +## Solutions |
| 46 | + |
| 47 | +### Solution 1: Greedy + Sorting |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +<!-- tabs:start --> |
| 54 | + |
| 55 | +```python |
| 56 | +class Solution: |
| 57 | + def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int: |
| 58 | + capacity.sort(reverse=True) |
| 59 | + s = sum(apple) |
| 60 | + for i, c in enumerate(capacity, 1): |
| 61 | + s -= c |
| 62 | + if s <= 0: |
| 63 | + return i |
| 64 | +``` |
| 65 | + |
| 66 | +```java |
| 67 | +class Solution { |
| 68 | + public int minimumBoxes(int[] apple, int[] capacity) { |
| 69 | + Arrays.sort(capacity); |
| 70 | + int s = 0; |
| 71 | + for (int x : apple) { |
| 72 | + s += x; |
| 73 | + } |
| 74 | + for (int i = 1, n = capacity.length;; ++i) { |
| 75 | + s -= capacity[n - i]; |
| 76 | + if (s <= 0) { |
| 77 | + return i; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + int minimumBoxes(vector<int>& apple, vector<int>& capacity) { |
| 88 | + sort(capacity.rbegin(), capacity.rend()); |
| 89 | + int s = accumulate(apple.begin(), apple.end(), 0); |
| 90 | + for (int i = 1;; ++i) { |
| 91 | + s -= capacity[i - 1]; |
| 92 | + if (s <= 0) { |
| 93 | + return i; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +}; |
| 98 | +``` |
| 99 | +
|
| 100 | +```go |
| 101 | +func minimumBoxes(apple []int, capacity []int) int { |
| 102 | + sort.Ints(capacity) |
| 103 | + s := 0 |
| 104 | + for _, x := range apple { |
| 105 | + s += x |
| 106 | + } |
| 107 | + for i := 1; ; i++ { |
| 108 | + s -= capacity[len(capacity)-i] |
| 109 | + if s <= 0 { |
| 110 | + return i |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +```ts |
| 117 | +function minimumBoxes(apple: number[], capacity: number[]): number { |
| 118 | + capacity.sort((a, b) => b - a); |
| 119 | + let s = apple.reduce((acc, cur) => acc + cur, 0); |
| 120 | + for (let i = 1; ; ++i) { |
| 121 | + s -= capacity[i - 1]; |
| 122 | + if (s <= 0) { |
| 123 | + return i; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +<!-- tabs:end --> |
| 130 | + |
| 131 | +<!-- end --> |
0 commit comments