FastPrepMinimum Cost to Remove Stones

Minimum Cost to Remove Stones

Airbnb logoAirbnb● HardFULLTIMEOA
Learn

Problem statement

There are n stones in a row, indexed from 0 to n - 1. Stone i has two associated costs: oneNeighborCost[i] and twoNeighborCost[i].

Remove every stone in any order. At the moment stone i is removed, its cost is:

  • twoNeighborCost[i] if both immediately adjacent stones i - 1 and i + 1 still exist.
  • oneNeighborCost[i] if exactly one of those immediately adjacent stones still exists.
  • 0 if neither immediately adjacent stone still exists.

An index outside the row does not contain a stone. Return the minimum possible total cost of removing all stones.

Function

minimumRemovalCost(oneNeighborCost: int[], twoNeighborCost: int[]) → long

Examples

Example 1

oneNeighborCost = [3,4,5]twoNeighborCost = [10,1,10]return = 1

Remove stone 1 first for cost 1. Its removal leaves both end stones without an immediate neighbor, so both are then removed for free.

Example 2

oneNeighborCost = [1,10,1]twoNeighborCost = [5,1,5]return = 1

Remove the middle stone first for cost 1. Both end stones are then isolated and can be removed for free.

Constraints

  • 1 <= n <= 5 * 10^4.
  • oneNeighborCost.length == twoNeighborCost.length == n.
  • 1 <= oneNeighborCost[i], twoNeighborCost[i] <= 1000.

More Airbnb problems

See Airbnb hiring insights
public long minimumRemovalCost(int[] oneNeighborCost, int[] twoNeighborCost) {
  // Write your code here.
}
oneNeighborCost[3,4,5]
twoNeighborCost[10,1,10]
expected1
Checking account…