Minimum Path Sum With Grid Switches
Updated
Problem 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.
More Infosys Onsite Interview questions
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
- Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
- Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
- Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
- Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
- Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
- Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026