3074. Apple Redistribution into Boxes LeetCode Solution

In this guide, you will get 3074. Apple Redistribution into Boxes LeetCode Solution with the best time and space complexity. The solution to Apple Redistribution into Boxes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Apple Redistribution into Boxes solution in C++
  4. Apple Redistribution into Boxes solution in Java
  5. Apple Redistribution into Boxes solution in Python
  6. Additional Resources
3074. Apple Redistribution into Boxes LeetCode Solution image

Problem Statement of Apple Redistribution into Boxes

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.

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})

3074. Apple Redistribution into Boxes LeetCode Solution in C++

class Solution {
 public:
  int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
    const int appleSum = accumulate(apple.begin(), apple.end(), 0);
    int capacitySum = 0;

    ranges::sort(capacity, greater<>());

    for (int i = 0; i < capacity.size(); ++i) {
      capacitySum += capacity[i];
      if (capacitySum >= appleSum)
        return i + 1;
    }

    return capacity.size();
  }
};
/* code provided by PROGIEZ */

3074. Apple Redistribution into Boxes LeetCode Solution in Java

class Solution {
  public int minimumBoxes(int[] apple, int[] capacity) {
    final int appleSum = Arrays.stream(apple).sum();
    int capacitySum = 0;

    Arrays.sort(capacity);

    for (int i = 0; i < capacity.length; ++i) {
      capacitySum += capacity[capacity.length - 1 - i];
      if (capacitySum >= appleSum)
        return i + 1;
    }

    return capacity.length;
  }
}
// code provided by PROGIEZ

3074. Apple Redistribution into Boxes LeetCode Solution in Python

class Solution:
  def minimumBoxes(self, apple: list[int], capacity: list[int]) -> int:
    appleSum = sum(apple)
    capacitySum = 0

    for i, c in enumerate(sorted(capacity, reverse=True)):
      capacitySum += c
      if capacitySum >= appleSum:
        return i + 1

    return len(capacity)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.