Minimum Path Sum With Grid Switches
Learn this problemProblem statement
You are given two integer grids grid1 and grid2 with the same dimensions, and an integer switchCost.
You need to travel from the top-left cell (0, 0) to the bottom-right cell. From a cell, you may move only one cell to the right or one cell down.
You may use either grid for the path. At any cell, you may switch from the current grid to the other grid at the same row and column by paying switchCost. Switching does not add the current cell's value a second time; it only changes which grid is used for subsequent moves.
Return the minimum possible path sum, including all visited cell values and any switch costs paid.
Function
minPathSumWithGridSwitches(grid1: int[][], grid2: int[][], switchCost: int) β intExamples
Example 1
grid1 = [[1, 3], [4, 2]]grid2 = [[2, 1], [1, 5]]switchCost = 3return = 6Stay on grid1 and take the path (0,0) -> (0,1) -> (1,1), with sum 1 + 3 + 2 = 6.
Example 2
grid1 = [[1, 100, 100], [1, 100, 1]]grid2 = [[100, 1, 1], [100, 1, 1]]switchCost = 2return = 6Start on grid1, move down to (1,0), switch to grid2, then move right twice. The cost is 1 + 1 + 2 + 1 + 1 = 6.
Constraints
grid1andgrid2are non-empty grids with the same dimensions.- The grids contain integer path costs.
switchCostis an integer cost for changing grids at a cell.