Desert Transport: Maximum Cargo Delivery with Consumption Cost
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Tests piecewise optimization and depot strategy when a vehicle with limited capacity must transport goods while consuming resources proportional to distance traveled.
A transport vehicle must move cargo units from an origin point to a destination across a route. The vehicle operates under the following rules:
- The vehicle has a carrying capacity: it can hold at most
capacityunits at once. - Moving the vehicle costs resources: for each unit of distance traveled, the vehicle consumes one cargo unit from its current load (or from units it picks up). This consumption applies to every trip, in both directions, regardless of how much cargo is being carried.
- The vehicle may create intermediate storage points: it can unload cargo at any location along the route and return to pick it up on subsequent trips.
- After delivering cargo to the destination, the vehicle does not need to return.
Given bananas units of cargo at the origin, a vehicle with carrying capacity, and a distance to the destination, determine the maximum number of cargo units that can be delivered.
You may assume the vehicle can stop at fractional distance points.
Constraints
0 ≤ bananas ≤ 10^60 < capacity ≤ 10^60 ≤ distance ≤ 10^6- Return a float representing the maximum deliverable cargo
- If delivery is impossible, return
0.0
Examples
Example 1
Input: max_cargo_delivered(3000, 1000, 1000)
Output: 533.3333333333333
Hints
Hint 1
Consider how the transportation strategy changes as the total cargo decreases below certain thresholds related to the capacity.
Hint 2
For a pile requiring k full loads to move, calculate how many distance units the vehicle travels per unit of forward progress.
Hint 3
Each phase ends when the pile shrinks enough to require one fewer load, allowing you to compute optimal depot locations.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to divide the journey into phases based on how many trips are needed to move the entire pile forward.
When you have enough cargo to require k loads (more than (k-1) × capacity units), each unit of forward progress costs 2k - 1 cargo units: the vehicle makes k forward trips and (k-1) return trips. Each of these trips consumes one unit per distance unit.
The optimal strategy:
- While the pile requires multiple loads, establish a depot just far enough forward that the pile shrinks to require one fewer load
- Move to this depot by shuttling cargo in k loads
- Repeat until the pile fits in a single load
- Make the final trip directly to the destination
For each phase:
- Calculate how many loads k are currently needed:
ceil(remaining / capacity) - The phase ends when remaining cargo equals
(k-1) × capacity - Distance for this phase:
(remaining - threshold) / (2k - 1) - Update remaining cargo and distance traveled
After the pile fits in one load, simply subtract the remaining distance from the cargo.
Why This Is Optimal
The cost per unit of forward progress is determined by the number of loads. By transitioning to fewer loads as soon as possible, we minimize total consumption. Any depot placed differently would either waste cargo by moving past the transition point or require extra trips.
Complexity
- Time: O(log(bananas / capacity)) — the number of phases equals the number of loads, which decreases geometrically
- Space: O(1) — only a few variables track state
Solution
import math
def max_cargo_delivered(bananas: int, capacity: int, distance: int) -> float:
if bananas <= 0 or capacity <= 0 or distance < 0:
return 0.0
remaining = float(bananas)
traveled = 0.0
# When the pile requires k loads, moving it forward by one unit
# costs (2k - 1) units: k forward trips and (k - 1) return trips
while remaining > capacity:
loads = math.ceil(remaining / capacity)
threshold = (loads - 1) * capacity
segment = (remaining - threshold) / (2 * loads - 1)
if traveled + segment >= distance:
return max(0.0, remaining - (distance - traveled) * (2 * loads - 1))
remaining = float(threshold)
traveled += segment
return max(0.0, remaining - (distance - traveled))