2304. Minimum Path Cost in a Grid LeetCode Solution

In this guide, you will get 2304. Minimum Path Cost in a Grid LeetCode Solution with the best time and space complexity. The solution to Minimum Path Cost in a Grid problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Path Cost in a Grid solution in C++
  4. Minimum Path Cost in a Grid solution in Java
  5. Minimum Path Cost in a Grid solution in Python
  6. Additional Resources
2304. Minimum Path Cost in a Grid LeetCode Solution image

Problem Statement of Minimum Path Cost in a Grid

You are given a 0-indexed m x n integer matrix grid consisting of distinct integers from 0 to m * n – 1. You can move in this matrix from a cell to any other cell in the next row. That is, if you are in cell (x, y) such that x < m – 1, you can move to any of the cells (x + 1, 0), (x + 1, 1), …, (x + 1, n – 1). Note that it is not possible to move from cells in the last row.
Each possible move has a cost given by a 0-indexed 2D array moveCost of size (m * n) x n, where moveCost[i][j] is the cost of moving from a cell with value i to a cell in column j of the next row. The cost of moving from cells in the last row of grid can be ignored.
The cost of a path in grid is the sum of all values of cells visited plus the sum of costs of all the moves made. Return the minimum cost of a path that starts from any cell in the first row and ends at any cell in the last row.

See also  2135. Count Words Obtained After Adding a Letter LeetCode Solution

Example 1:

Input: grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
Output: 17
Explanation: The path with the minimum possible cost is the path 5 -> 0 -> 1.
– The sum of the values of cells visited is 5 + 0 + 1 = 6.
– The cost of moving from 5 to 0 is 3.
– The cost of moving from 0 to 1 is 8.
So the total cost of the path is 6 + 3 + 8 = 17.

Example 2:

Input: grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
Output: 6
Explanation: The path with the minimum possible cost is the path 2 -> 3.
– The sum of the values of cells visited is 2 + 3 = 5.
– The cost of moving from 2 to 3 is 1.
So the total cost of this path is 5 + 1 = 6.

Constraints:

m == grid.length
n == grid[i].length
2 <= m, n <= 50
grid consists of distinct integers from 0 to m * n – 1.
moveCost.length == m * n
moveCost[i].length == n
1 <= moveCost[i][j] <= 100

Complexity Analysis

  • Time Complexity: O(mn^2)
  • Space Complexity: O(mn)

2304. Minimum Path Cost in a Grid LeetCode Solution in C++

class Solution {
 public:
  int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
    const int m = grid.size();
    const int n = grid[0].size();
    // dp[i][j] := the minimum cost to reach grid[i][j]
    vector<vector<int>> dp(m, vector<int>(n, INT_MAX));
    dp[0] = grid[0];

    for (int i = 1; i < m; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + moveCost[grid[i - 1][k]][j] +
                                       grid[i][j]);

    return ranges::min(dp.back());
  }
};
/* code provided by PROGIEZ */

2304. Minimum Path Cost in a Grid LeetCode Solution in Java

class Solution {
  public int minPathCost(int[][] grid, int[][] moveCost) {
    final int m = grid.length;
    final int n = grid[0].length;
    // dp[i][j] := the minimum cost to reach grid[i][j]
    int[][] dp = new int[m][n];
    Arrays.stream(dp).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
    dp[0] = grid[0];

    for (int i = 1; i < m; ++i)
      for (int j = 0; j < n; ++j)
        for (int k = 0; k < n; ++k)
          dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + moveCost[grid[i - 1][k]][j] + grid[i][j]);

    return Arrays.stream(dp[m - 1]).min().getAsInt();
  }
}
// code provided by PROGIEZ

2304. Minimum Path Cost in a Grid LeetCode Solution in Python

class Solution:
  def minPathCost(
      self,
      grid: list[list[int]],
      moveCost: list[list[int]],
  ) -> int:
    m = len(grid)
    n = len(grid[0])
    # dp[i][j] := the minimum cost to reach grid[i][j]
    dp = [[math.inf] * n for _ in range(m)]
    dp[0] = grid[0]

    for i in range(1, m):
      for j in range(n):
        for k in range(n):
          dp[i][j] = min(dp[i][j], dp[i - 1][k] +
                         moveCost[grid[i - 1][k]][j] + grid[i][j])

    return min(dp[-1])
# code by PROGIEZ

Additional Resources

See also  2327. Number of People Aware of a Secret LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.