Back to Practice
#0133
Minimum Path Sum
MediumDSA25 min20 XP
Problem
Given a grid of non-negative costs, return the minimum path sum from top-left to bottom-right moving only right or down.
Why This Matters
This is the weighted version of unique paths and a core 2D DP pattern.
Function Signature
def min_path_sum(grid):
Examples
Example 1
Input[[1,3,1],[1,5,1],[4,2,1]]
Output7
The cheapest path is 1 -> 3 -> 1 -> 1 -> 1.
Constraints
- Return the exact requested structure.
- Handle normal edge cases cleanly.
- Prefer readable code over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[[1,3,1],[1,5,1],[4,2,1]]
Expected7
The cheapest path is 1 -> 3 -> 1 -> 1 -> 1.
Hidden Test Categories
single rowsingle column2x2 grid