Minimum Path Sum - Problem
Minimum Path Sum
You are given an
Movement Rules:
• You can only move right or down at any point
• You cannot move diagonally, left, or up
Example:
For grid
You are given an
m x n grid filled with non-negative numbers. Your goal is to find a path from the top-left corner to the bottom-right corner that minimizes the sum of all numbers along the path.Movement Rules:
• You can only move right or down at any point
• You cannot move diagonally, left, or up
Example:
For grid
[[1,3,1],[1,5,1],[4,2,1]], the minimum path sum is 7 following the path: 1→3→1→1→1 Input & Output
example_1.py — Basic 3x3 Grid
$
Input:
grid = [[1,3,1],[1,5,1],[4,2,1]]
›
Output:
7
💡 Note:
The path 1→3→1→1→1 gives minimum sum of 7. Alternative paths like 1→1→5→1→1 would give sum of 9, which is larger.
example_2.py — 2x3 Grid
$
Input:
grid = [[1,2,3],[4,5,6]]
›
Output:
12
💡 Note:
The minimum path is 1→2→3→6, giving sum of 12. Going 1→4→5→6 would give 16.
example_3.py — Single Element
$
Input:
grid = [[5]]
›
Output:
5
💡 Note:
Edge case: grid with only one element. The minimum (and only) path sum is the element itself.
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 200
- 0 ≤ grid[i][j] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Start the Quest
Begin at entrance (top-left) with initial cost
2
Explore Options
At each room, can only move right or down
3
Calculate Costs
Each room's minimum cost = room cost + min(cost from top, cost from left)
4
Reach Treasure
Arrive at treasure room (bottom-right) with minimum total cost
Key Takeaway
🎯 Key Insight: Use dynamic programming to build up the solution - the minimum cost to reach any cell is its own cost plus the minimum of costs from its only two possible predecessors (top and left cells).
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code