Capacity to Ship Packages Within Days
Updated
Problem statement
Packages must be shipped in the order given by weights. Each day, load a consecutive part from the beginning of the remaining packages without exceeding the ship's capacity.
Return the smallest positive capacity that ships every package within days days. Some days may be unused, including when there are more days than packages. The required capacity may exceed a 32-bit integer.
This is also the ordered task-allocation problem: package weights represent task durations, days represent available resources, and each resource receives one contiguous group of tasks. The capacity is the maximum total duration assigned to any resource.
Function
shipWithinDays(weights: int[], days: int) → longExamples
Example 1
weights = [1,2,3,4,5,6,7,8,9,10]days = 5return = 15Capacity 15 ships the ordered packages in five days, while any smaller capacity needs more days.
Example 2
weights = [3,2,2,4,1,4]days = 3return = 6The loads can be [3,2], [2,4], and [1,4].
Example 3
weights = [1,2,3,1,1]days = 4return = 3Capacity 3 is enough and no capacity below the heaviest package can work.
Example 4
weights = [4,3,2,2,2,6]days = 3return = 7The groups [4,3], [2,2,2], and [6] have loads 7, 6, 6. Capacity 6 would need four groups: [4], [3,2], [2,2], and [6]. The minimum is 7.
Example 5
weights = [9,2,4,4,5]days = 2return = 13Split into [9,2] and [4,4,5], with loads 11 and 13. At capacity 12, the greedy groups are [9,2], [4,4], and [5], so two days are insufficient. The minimum is 13.
Constraints
1 <= weights.length <= 10^5.1 <= weights[i] <= 10^9.1 <= days <= 10^5.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Daily Printing LimitAgoda · ONSITE INTERVIEWAgoda interview questions
- Minimize the Longest Hike Between Rest StopsPinterest · ONSITE INTERVIEWPinterest interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Robot and Human Fulfillment TimeAmazon · OA
- Minimize Maximum Pilot WorkloadSpotnana · ONSITE INTERVIEWSpotnana interview questions
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Array NullificationQRT · OA
- Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions
- Three-Part Partition with Minimum Largest RangeLSEG · OALSEG interview questions
- K-Means ClusteringAtlassian · OAAtlassian interview questions