logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
2. Add Two Numbers3. Longest Substring Without Repeating Characters5. Longest Palindromic Substring8. String to Integer (atoi)11. Container With Most Water15. 3Sum17. Letter Combinations of a Phone Number19. Remove Nth Node From End of List22. Generate Parentheses33. Search in Rotated Sorted Array34. Find First and Last Position of Element in Sorted Array48. Rotate Image49. Group Anagrams50. Pow(x, n)53. Maximum Subarray54. Spiral Matrix56. Merge Intervals61. Rotate List73. Set Matrix Zeroes74. Search a 2D Matrix75. Sort Colors78. Subsets137. Single Number II138. Copy List with Random Pointer150. Evaluate Reverse Polish Notation151. Reverse Words in a String153. Find Minimum in Rotated Sorted Array155. Min Stack162. Find Peak Element169. Majority Element198. House Robber207. Course Schedule215. Kth Largest Element in an Array227. Basic Calculator II236. Lowest Common Ancestor of a Binary Tree237. Delete Node in a Linked List238. Product of Array Except Self451. Sort Characters By Frequency540. Single Element in a Sorted Array560. Subarray Sum Equals K636. Exclusive Time of Functions875. Koko Eating Bananas1011. Capacity To Ship Packages Within D Days1101. The Earliest Moment When Everyone Become Friends1283. Find the Smallest Divisor Given a Threshold2140. Solving Questions With Brainpower3169. Count Days Without Meetings3481. Apply Substitutions
Code Scratch/leetcode/medium

1011. Capacity To Ship Packages Within D Days

2 min read
On this pageIntuition
IntuitionComplexityCode

# Intuition

We need to find the minimum ship capacity such that all packages are shipped within a given number of days. Since packages must be loaded in order and cannot exceed the ship's capacity each day, increasing the ship's capacity can reduce the number of days needed.

This forms a monotonic search space, where increasing the capacity monotonically decreases the required number of days. Hence, we can apply binary search on the capacity range.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(N∗log⁡N)\text{O}(N*\log{N})O(N∗logN)

# Code

// Helper function to calculate the number of days required to ship all packages with given ship capacity
public int daysToShip(int[] packageWeights, int shipCapacity) {
    int currentLoad = 0, requiredDays = 0;
 
    for (int weight : packageWeights) {
        // If adding current weight exceeds the capacity, ship the load and start a new day
        if (currentLoad + weight > shipCapacity) {
            currentLoad = 0;
            requiredDays++;
        }
        currentLoad += weight;
    }
 
    // Account for the last day's shipment
    if (currentLoad > 0) requiredDays++;
 
    return requiredDays;
}
 
public int shipWithinDays(int[] packageWeights, int maxAllowedDays) {
    int minCapacity = 0, maxCapacity = 0, optimalCapacity = 0;
 
    // Initialize search bounds
    for (int weight : packageWeights) {
        minCapacity = Math.max(minCapacity, weight); // at least the heaviest package
        maxCapacity += weight; // at most all weights in one day
    }
 
    // Binary search for the minimum sufficient capacity
    while (minCapacity <= maxCapacity) {
        int midCapacity = (minCapacity + maxCapacity) / 2;
 
        int daysNeeded = daysToShip(packageWeights, midCapacity);
 
        if (daysNeeded <= maxAllowedDays) {
            // Capacity is sufficient, try to find a smaller one
            optimalCapacity = midCapacity;
            maxCapacity = midCapacity - 1;
        } else {
            // Not enough capacity, try a larger one
            minCapacity = midCapacity + 1;
        }
    }
 
    return optimalCapacity;
}
 
←Previous875. Koko Eating BananasNext→1101. The Earliest Moment When Everyone Become Friends

On this page

IntuitionComplexityCode