Minimum Cost to Remove Stones
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 stonesi - 1andi + 1still exist.oneNeighborCost[i]if exactly one of those immediately adjacent stones still exists.0if 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[]) → longExamples
Example 1
oneNeighborCost = [3,4,5]twoNeighborCost = [10,1,10]return = 1Remove 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 = 1Remove 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.