Command Palette
Search for a command to run...
Problem 29.3 · DP on GridsMedium
What it teaches: Cheapest path with min instead of sum of counts.
Practise it on judges as “Minimum Path Sum”.
The problem
Moving only right or down, find a path from the top-left to the bottom-right that minimises the sum of the numbers on it.
Example 1
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
1 → 3 → 1 → 1 → 1.
Constraints
- 1 ≤ m, n ≤ 200
- 0 ≤ values ≤ 200
Pattern clues in the wording
- → Minimum cost path with right/down moves
These clues point to Grid DP: Each cell's answer comes from the cells above and to the left; fill the grid row by row.
Stuck? Take one hint at a time
Solution.java · starterclass Solution {
public int minPathSum(int[][] grid) {
return 0;
}
}
Write your solution locally or in your editor for now. The in-browser runner (Java first, then Python, C++ and more) will run these tests right here.
Test cases
| # | Input | Expected |
|---|
| 1 | grid = [[1,3,1],[1,5,1],[4,2,1]] | 7 |
| 2 | grid = [[1,2,3],[4,5,6]] | 12 |