Algorithm


Problem Name: 1011. Capacity To Ship Packages Within D Days

A conveyor belt has packages that must be shipped from one port to another within days days.

The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

 

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10

Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.

Example 2:

Input: weights = [3,2,2,4,1,4], days = 3
Output: 6
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4

Example 3:

Input: weights = [1,2,3,1,1], days = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1

 

Constraints:

  • 1 <= days <= weights.length <= 5 * 104
  • 1 <= weights[i] <= 500

Code Examples

#1 Code Example with C Programming

Code - C Programming


int count_days(int *weight, int sz, int k) {
    int i, w = 0, d = 1;
    for (i = 0; i < sz; i ++) {
        if (w + weight[i] > k) {
            d ++;
            w = 0;
        }
        w += weight[i];
    }
    return d;
}
​
int shipWithinDays(int* weights, int weightsSize, int D){
    int left, right, mid;
    int i, w, d;
    
    left = right = 0;
    
    for (i = 0; i < weightsSize; i ++) {
        w = weights[i];
        if (left < w) left = w;
        right += w;
    }

    while (left < right) {
        mid = left + (right - left) / 2;
        d = count_days(weights, weightsSize, mid);
        if (d <= D) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    
    return left;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
weights = [1,2,3,4,5,6,7,8,9,10], days = 5

Output

x
+
cmd
15

#2 Code Example with Javascript Programming

Code - Javascript Programming


class Solution {
  public int shipWithinDays(int[] weights, int days) {
    int maximumWeight = 0;
    int minimumWeight = weights[0];
    for (int weight : weights) {
      maximumWeight += weight;
      minimumWeight = Math.max(minimumWeight, weight);
    }
    while (minimumWeight <= maximumWeight) {
      int currCapacity = (maximumWeight + minimumWeight) / 2;
      if (isShipmentPossible(weights, days, currCapacity)) {
        maximumWeight = currCapacity - 1;
      } else {
        minimumWeight = currCapacity + 1;
      }
    }
    return minimumWeight;
  }

  private boolean isShipmentPossible(int[] weights, int days, int capacity) {
    int idx = 0;
    while (idx < weights.length) {
      int currWeight = 0;
      while (idx < weights.length && currWeight + weights[idx] <= capacity) {
        currWeight += weights[idx++];
      }
      days--;
    }
    return days >= 0;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
weights = [1,2,3,4,5,6,7,8,9,10], days = 5

Output

x
+
cmd
15

#3 Code Example with Javascript Programming

Code - Javascript Programming


const shipWithinDays = function(weights, days) {
  let l = Math.max(...weights)
  let r = weights.reduce((ac, e) => ac + e, 0)
  while(l < r) {
     const mid = Math.floor((l + r) / 2)
     if(valid(mid)) {
       r = mid
     } else l = mid + 1
  }
  
  return l
  
  function valid(mid) {
    let res = 1, cur = 0
    for(let w of weights) {
      if(cur + w > mid) {
        cur = 0
        res++
      }
      cur += w
    }
    return res <= days
  }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
weights = [3,2,2,4,1,4], days = 3

Output

x
+
cmd
6

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def shipWithinDays(self, weights: List[int], D: int) -> int:
        def check(mx):
            days, cur = 1, 0
            for w in weights:
                if cur + w <= mx:
                    cur += w
                else:
                    days += 1
                    cur = w
            return days
            
        l, r = max(weights), sum(weights)
        while l < r:
            mid = (l + r) // 2
            days = check(mid)
            if days <= D:
                r = mid
            else:
                l = mid + 1
        return r
Copy The Code & Try With Live Editor

Input

x
+
cmd
weights = [3,2,2,4,1,4], days = 3

Output

x
+
cmd
6

#5 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _1011_CapacityToShipPackagesWithinDDays
    {
        public int ShipWithinDays(int[] weights, int D)
        {
            var totalSum = 0;
            var largestNumber = -1;
            for (int i = 0; i < weights.Length; i++)
            {
                largestNumber = Math.Max(largestNumber, weights[i]);
                totalSum += weights[i];
            }

            var l = largestNumber;
            var h = totalSum;
            while (l <= h)
            {
                var mid = l + (h - l) / 2;
                var count = 1;
                var currentSum = 0;
                for (int i = 0; i < weights.Length; i++)
                {
                    if (currentSum + weights[i] > mid)
                    {
                        count++;
                        if (count > D) break;
                        currentSum = weights[i];
                    }
                    else currentSum += weights[i];
                }
                if (count > D) l = mid + 1;
                else h = mid - 1;
            }
            return l;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
weights = [1,2,3,1,1], days = 4

Output

x
+
cmd
3

Demonstration


Previous
#1010 Leetcode Pairs of Songs With Total Durations Divisible by 60 Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1012 Leetcode Numbers With Repeated Digits Solution in C, C++, Java, JavaScript, Python, C# Leetcode