Container Distribution Count — Problem Statement & Solution Guide
Problem Description
Given an array of container capacities capacities and an array of ship capacities ships, find the total number of ways to distribute the containers among the ships such that the sum of container capacities does not exceed the ship's capacity and the number of containers does not exceed the ship's capacity. If no such distribution is possible, return 0.
Examples
Input
[1, 2, 3, 4, 5], [10, 10, 10]
Output
0
Explanation: Step-by-step: We have 5 containers with capacities 1, 2, 3, 4, 5 and 3 ships with capacities 10 each. We cannot distribute any container to any ship because the capacity of each ship is exceeded. Therefore, the total number of ways to distribute the containers among the ships is 0.
Input
[1, 2, 3, 4, 5], [6, 6, 6]
Output
0
Explanation: Step-by-step: We have 5 containers with capacities 1, 2, 3, 4, 5 and 3 ships with capacities 6 each. We cannot distribute any container to any ship because the capacity of each ship is exceeded. Therefore, the total number of ways to distribute the containers among the ships is 0.
Constraints
- 1 <= shipCapacity <= 100
- 1 <= containerCapacities.length <= 20
- 1 <= containerCapacities[i] <= 50
Optimal Approach & Strategy
The optimized approach uses recursion and backtracking to explore all possible distributions of containers among the ships, resulting in a time complexity of O(2^n). This approach is more efficient than the brute-force approach but still may not be practical for very large inputs.
Brute Force Approach
The brute-force approach would involve trying all possible combinations of containers on each ship, resulting in a time complexity of O(n!). This approach is impractical for large inputs due to its exponential time complexity.
Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.