Minimum Cost with Adjacent Choices Different
Problem statement
You are given a rectangular cost matrix costs. Choose exactly one column in each row and pay the cost at that position. Consecutive rows must choose different columns; nonconsecutive rows may reuse a column.
For this exercise, assume the matrix represents a sequence of items with the same available choices in every row. Return the minimum total cost satisfying the adjacent-choice rule. Return 0 for no rows and -1 when no valid sequence exists.
Function
minAdjacentChoiceCost(costs: int[][]) → intExamples
Example 1
costs = [[1,7],[4,2],[3,8]]return = 6Choose columns 0, 1 and 0, paying 1 + 2 + 3. Reusing column 0 after a gap is allowed.
Example 2
costs = [[3],[5]]return = -1Only one column is available, so two consecutive rows cannot choose different columns.
Example 3
costs = []return = 0There are no rows and no cost to pay.
Constraints
0 <= costs.length <= 80.- For nonempty input, every row has the same length
k, with1 <= k <= 16. 0 <= costs[r][c] <= 10^6.