BackmediumRecursion PayPal

Galactic Cargo Distribution Solution

Problem Statement

Given a list of cargo capacities and a ship's capacity, find the total number of ways to distribute the cargo among the ship, considering that a ship can only carry containers with capacities that sum up to the ship's capacity, and no ship can carry more containers than its capacity. If such a distribution is not possible, return 0. The cargo can be distributed among the same ship or not. The cargo can exceed the ship's capacity or not.

Example 1
Input
[1, 2, 3, 4, 5], 10
Output
1

Explanation: Step-by-step: We can distribute all containers among the same ship. The sum of capacities 1+2+3+4+5 = 15 exceeds the ship's capacity 10, but we can distribute all containers among the same ship by not using the last container.

Example 2
Input
[1, 2, 3], 6
Output
1

Explanation: Step-by-step: We can distribute the containers among the same ship. The sum of capacities 1+2+3 = 6 equals the ship's capacity. We can put the last container with capacity 4 in the same ship, but we cannot put it because it exceeds the ship's capacity.

Constraints

  • 1 <= shipCapacity <= 100
  • 1 <= containerCapacities.length <= 20
  • 1 <= containerCapacities[i] <= 50
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free