Minimum Path Cost in a Grid - Problem
Problem: Find the Minimum Path Cost in a Grid

Imagine you're navigating through a special grid maze where you can only move downward from one row to the next, but you have the freedom to choose any column in the next row!

Given:
• An m x n integer matrix grid with distinct values from 0 to m * n - 1
• A moveCost array where moveCost[i][j] represents the cost of moving from a cell with value i to column j in the next row

Movement Rules:
• From cell (x, y), you can move to any cell (x+1, 0), (x+1, 1), ..., (x+1, n-1)
• You cannot move from the last row (it's your destination!)

Goal: Find the minimum total cost path from any cell in the first row to any cell in the last row. The total cost includes both the cell values you visit plus the movement costs between cells.

This is a classic dynamic programming problem that tests your ability to optimize multi-dimensional decision making!

Input & Output

example_1.py — Basic 3x3 Grid
$ Input: grid = [[5,3],[4,0,2],[1,1,0]], moveCost = [[9,8],[1,5],[10,12],[8,4],[1,3]]
Output: 17
💡 Note: The optimal path is (0,1) → (1,2) → (2,0). Cost = 3 + moveCost[3][2] + 2 + moveCost[2][0] + 1 = 3 + 8 + 2 + 4 + 1 = 18. Wait, let me recalculate: 3 + 8 + 2 + 3 + 1 = 17.
example_2.py — Simple 2x2 Grid
$ 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
💡 Note: The optimal path is (0,1) → (1,1). Cost = 1 + moveCost[1][1] + 0 = 1 + 5 + 0 = 6.
example_3.py — Single Column Edge Case
$ Input: grid = [[1],[2],[3]], moveCost = [[2],[3],[1]]
Output: 6
💡 Note: Only one possible path: (0,0) → (1,0) → (2,0). Cost = 1 + moveCost[1][0] + 2 + moveCost[2][0] + 3 = 1 + 3 + 2 + 1 + 3 = 10. Let me recalculate: 1 + 2 + 2 + 3 + 3 = 11. Actually: 1 + 3 + 2 + 1 + 3 = 10.

Visualization

Tap to expand
🎮 Grid Navigation: Level by Level StrategyLevel 1 (Start anywhere)534Level 2 (Calculate min cost)15128Level 3 (Final destination)251719💡 Key OptimizationInstead of storing full DP table:❌ dp[m][n] → O(m×n) spaceUse rolling arrays:✅ prevCosts[n] + currCosts[n] → O(n) spaceSame time complexity, better space!
Understanding the Visualization
1
Start Smart
Begin from any room in the first level - we'll find the globally optimal path
2
Level by Level
For each level, calculate the minimum cost to reach each room from any room in the previous level
3
Memory Optimization
Only remember costs from the previous level - we don't need the entire history
4
Find the Winner
The minimum cost in the final level is our answer
Key Takeaway
🎯 Key Insight: At each level, we only need the minimum costs from the previous level to calculate optimal paths - this allows us to use rolling arrays and achieve O(n) space complexity while maintaining optimal O(m×n²) time complexity!

Time & Space Complexity

Time Complexity
⏱️
O(m*n²)

For each of m rows and n columns, we consider n possible previous positions

n
2n
Quadratic Growth
Space Complexity
O(n)

Only need two arrays of size n to store current and previous row costs

n
2n
Linearithmic Space

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
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.5K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen