Problem · Dynamic Programming

Factory Cost - Min-Cost Path Skipping One Stage

HardStripePHONE SCREEN
See Stripe hiring insights

This continues the factory min-cost-path problem. stages[i] is a list of factory choices for stage i, each choice being [build_cost, position] on a 1-D line. The total cost of a selection is sum(chosen build_cost) + sum(|pos_i - pos_{i+1}|) over consecutive chosen factories, with no inbound transit for the first chosen factory.

Now you must remove exactly one stage before selecting. After a stage is removed, the remaining stages are taken in order, and the transit cost is measured across the gap — between the factory chosen in the stage before the removed one and the factory chosen in the stage after it.

Try removing every stage and return the minimum achievable total. If there are 1 or fewer stages, return 0 (after removal there is nothing left to connect, so transit is 0).

Examples
01 · Example 1
stages = [[[10,0],[20,2]], [[100,5]], [[15,1],[25,3]], [[5,2],[15,0]]]
return = 32
Removing stage index 1 (the expensive single factory [100,5]) is best. From the remaining stages pick [10,0] -> [15,1] -> [5,2]: build = 10 + 15 + 5 = 30, transit = |0-1| + |1-2| = 2, total = 32. No other stage removal yields a lower total.
02 · Example 2
stages = [[[10,0],[20,2]], [[100,5]]]
return = 10
With 2 stages, removing one leaves a single stage, so there is no transit. Removing stage 1 leaves stage 0, whose cheapest factory build cost is 10. Removing stage 0 leaves stage 1 with build cost 100. The minimum is 10.
Constraints
  • stages[i] is a non-empty list of [build_cost, position] integer choices.
  • Exactly one stage is removed before selecting one factory per remaining stage.
  • Transit across the removed stage's gap is measured between the chosen factories of the stages immediately before and after it.
  • If stages.length <= 1, return 0.
  • Either the first or last stage may be the one removed.
More Stripe problems
drafts saved locally
public int findMinimumCostSkipOne(int[][][] stages) {
  // write your code here
}
stages[[[10,0],[20,2]], [[100,5]], [[15,1],[25,3]], [[5,2],[15,0]]]
expected32
sign in to submit