Problem

Minimum Path Sum With Grid Switches

infosysONSITE INTERVIEW

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.

Examples
01 Β· Example 1
grid1 = [[1, 3], [4, 2]]
grid2 = [[2, 1], [1, 5]]
switchCost = 3
return = 6

Stay on grid1 and take the path (0,0) -> (0,1) -> (1,1), with sum 1 + 3 + 2 = 6.

02 Β· Example 2
grid1 = [[1, 100, 100], [1, 100, 1]]
grid2 = [[100, 1, 1], [100, 1, 1]]
switchCost = 2
return = 6

Start on grid1, move down to (1,0), switch to grid2, then move right twice. The cost is 1 + 1 + 2 + 1 + 1 = 6.

Constraints
  • grid1 and grid2 are non-empty grids with the same dimensions.
  • The grids contain integer path costs.
  • switchCost is an integer cost for changing grids at a cell.
More infosys problems
drafts saved locally
public int minPathSumWithGridSwitches(int[][] grid1, int[][] grid2, int switchCost) {
  // write your code here
}
grid1[[1, 3], [4, 2]]
grid2[[2, 1], [1, 5]]
switchCost3
expected6
sign in to submit