Problem 347033 · medium · Phase 03 Linear Management & Searching

Smallest Truck for a Fixed Number of Trips

binary search on the answer · greedy

Parcels must be delivered in the given order; weights[i] is the weight of parcel i. A truck with capacity c loads consecutive parcels until the next one would exceed c, then makes a trip. Return the smallest integer capacity that delivers everything in at most trips trips.

Examples

Input:  weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], trips = 5
Output: 15
Explanation: loads [1..5], [6, 7], [8], [9], [10].

Input:  weights = [3, 2, 2, 4, 1, 4], trips = 3
Output: 6

Input:  weights = [1, 2, 3, 1, 1], trips = 4
Output: 3

Constraints

  • 1 <= len(weights) <= 5 * 10**4, 1 <= trips <= len(weights)
  • 1 <= weights[i] <= 500
  • Required time: O(n log(sum(weights))).

Goals

  • Choose the correct lower and upper bounds for the answer range
  • Simulate greedy loading as the feasibility check
Starting Python…