Capacity To Ship Packages Within D Days in Python

The Capacity to Ship Packages Within D Days problem asks us to find the minimum ship capacity needed to transport all packages within a given number of days. Given an array of package weights and D days, we need to determine the least weight capacity that allows shipping all packages sequentially.

Problem Understanding

For example, with weights [3,2,2,4,1,4] and D = 3 days, the minimum capacity is 6 ?

  • Day 1: 3, 2 (total weight: 5)

  • Day 2: 2, 4 (total weight: 6)

  • Day 3: 1, 4 (total weight: 5)

Algorithm Approach

We use binary search on the answer. The minimum capacity must be at least the heaviest package, and the maximum would be the sum of all weights. For each candidate capacity, we check if it's possible to ship all packages within D days.

Helper Function

The can_ship function checks if a given capacity allows shipping within D days ?

def can_ship(weights, capacity, days):
    current_weight = 0
    days_used = 1
    
    for weight in weights:
        if current_weight + weight <= capacity:
            current_weight += weight
        else:
            days_used += 1
            current_weight = weight
            if days_used > days:
                return False
    
    return True

# Test the helper function
weights = [3, 2, 2, 4, 1, 4]
print("Can ship with capacity 6 in 3 days:", can_ship(weights, 6, 3))
print("Can ship with capacity 5 in 3 days:", can_ship(weights, 5, 3))
Can ship with capacity 6 in 3 days: True
Can ship with capacity 5 in 3 days: False

Complete Solution

def ship_within_days(weights, days):
    def can_ship(weights, capacity, days):
        current_weight = 0
        days_used = 1
        
        for weight in weights:
            if current_weight + weight <= capacity:
                current_weight += weight
            else:
                days_used += 1
                current_weight = weight
                if days_used > days:
                    return False
        
        return True
    
    # Binary search bounds
    left = max(weights)  # Minimum possible capacity
    right = sum(weights)  # Maximum possible capacity
    
    while left < right:
        mid = left + (right - left) // 2
        
        if can_ship(weights, mid, days):
            right = mid  # Try smaller capacity
        else:
            left = mid + 1  # Need larger capacity
    
    return left

# Test with the example
weights = [3, 2, 2, 4, 1, 4]
days = 3
result = ship_within_days(weights, days)
print(f"Minimum ship capacity: {result}")

# Test with another example
weights2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
days2 = 5
result2 = ship_within_days(weights2, days2)
print(f"Minimum ship capacity for second example: {result2}")
Minimum ship capacity: 6
Minimum ship capacity for second example: 15

How It Works

The algorithm uses binary search on the capacity range:

  1. Lower bound: Maximum weight (heaviest single package)

  2. Upper bound: Sum of all weights (ship all in one day)

  3. Check function: For each capacity, simulate loading packages day by day

  4. Binary search: If current capacity works, try smaller; otherwise, try larger

Time Complexity

The time complexity is O(n × log(sum - max)), where n is the number of packages. The binary search runs in O(log(sum - max)) iterations, and each iteration requires O(n) time to check if shipping is possible.

Conclusion

This problem efficiently uses binary search to find the minimum ship capacity. The key insight is that if a capacity works, all larger capacities also work, making binary search applicable.

Updated on: 2026-03-25T08:14:09+05:30

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements