KatInterview

Desert Transport: Maximum Cargo Delivery with Consumption Cost

Math & Bit ManipulationAuto-graded · 21 test cases
MediumAuto-gradedMath & Bit Manipulationoptimizationmathgreedy

Last 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 capacity units 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^6
  • 0 < capacity ≤ 10^6
  • 0 ≤ 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.