FastPrepMinimum Cost with Adjacent Choices Different

Minimum Cost with Adjacent Choices Different

Roblox logoRoblox● MediumNEW GRADOA
Learn

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[][]) → int

Examples

Example 1

costs = [[1,7],[4,2],[3,8]]return = 6

Choose columns 0, 1 and 0, paying 1 + 2 + 3. Reusing column 0 after a gap is allowed.

Example 2

costs = [[3],[5]]return = -1

Only one column is available, so two consecutive rows cannot choose different columns.

Example 3

costs = []return = 0

There are no rows and no cost to pay.

Constraints

  • 0 <= costs.length <= 80.
  • For nonempty input, every row has the same length k, with 1 <= k <= 16.
  • 0 <= costs[r][c] <= 10^6.

More Roblox problems

See Roblox hiring insights
public int minAdjacentChoiceCost(int[][] costs) {
    // write your code here
}
costs[[1,7],[4,2],[3,8]]
expected6
Checking account…