Minimum Path Cost in a Grid

medium dp matrix

Problem

You are given an m×n integer grid where every cell holds a distinct value from 0 to m·n−1. Starting from any cell in the top row, you walk down one row at a time until you reach the bottom row. Moving from a cell into column j of the next row costs moveCost[v][j], where v is the value of the cell you are leaving. The total cost of a path is the sum of all the cell values it visits plus the cost of every move. Return the minimum possible total cost of any top-to-bottom path.

Inputgrid = [[5,3],[4,0],[2,1]], moveCost row per value 0..5
Output17
Path 5 → 0 → 1 collects cells 5 + 0 + 1 = 6, and the two move costs add 11, for a total of 17 — the cheapest possible.

def minPathCost(grid, moveCost):
    m, n = len(grid), len(grid[0])
    dp = grid[0][:]
    for r in range(1, m):
        nxt = [float('inf')] * n
        for c in range(n):
            for p in range(n):
                cost = dp[p] + moveCost[grid[r - 1][p]][c] + grid[r][c]
                if cost < nxt[c]:
                    nxt[c] = cost
        dp = nxt
    return min(dp)
function minPathCost(grid, moveCost) {
  const m = grid.length, n = grid[0].length;
  let dp = grid[0].slice();
  for (let r = 1; r < m; r++) {
    const nxt = new Array(n).fill(Infinity);
    for (let c = 0; c < n; c++) {
      for (let p = 0; p < n; p++) {
        const cost = dp[p] + moveCost[grid[r - 1][p]][c] + grid[r][c];
        if (cost < nxt[c]) nxt[c] = cost;
      }
    }
    dp = nxt;
  }
  return Math.min(...dp);
}
class Solution {
    public int minPathCost(int[][] grid, int[][] moveCost) {
        int m = grid.length, n = grid[0].length;
        int[] dp = grid[0].clone();
        for (int r = 1; r < m; r++) {
            int[] nxt = new int[n];
            for (int c = 0; c < n; c++) {
                nxt[c] = Integer.MAX_VALUE;
                for (int p = 0; p < n; p++) {
                    int cost = dp[p] + moveCost[grid[r - 1][p]][c] + grid[r][c];
                    if (cost < nxt[c]) nxt[c] = cost;
                }
            }
            dp = nxt;
        }
        int ans = Integer.MAX_VALUE;
        for (int v : dp) ans = Math.min(ans, v);
        return ans;
    }
}
int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
    int m = (int)grid.size(), n = (int)grid[0].size();
    vector<int> dp = grid[0];
    for (int r = 1; r < m; r++) {
        vector<int> nxt(n, INT_MAX);
        for (int c = 0; c < n; c++) {
            for (int p = 0; p < n; p++) {
                int cost = dp[p] + moveCost[grid[r - 1][p]][c] + grid[r][c];
                if (cost < nxt[c]) nxt[c] = cost;
            }
        }
        dp = nxt;
    }
    return *min_element(dp.begin(), dp.end());
}
Time: O(m·n²) Space: O(n)