Minimum Path Sum
A grid holds a cost in every cell. Go from the top-left corner to the bottom-right one, stepping only right or down, and pay for every cell you stand on, both corners included. Find the smallest total. Our example: [3, 1, 5, 1], [2, 6, 4, 2], [1, 1, 3, 2], the answer is 12.
- input
grid: anm × ngrid of non-negative numbers.- returns
- The smallest sum of the numbers on a path from the top-left cell to the bottom-right one that moves only right or down.
- constraints
- 1 ≤ m = grid.length ≤ 100
- 1 ≤ n = grid[i].length ≤ 100
- 0 ≤ grid[i][j] ≤ 200
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[][]} grid
* @return {number}
*/
function minPathSum(grid) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 198House RobberMediumsolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 139Word BreakMediumsolve
- 120TriangleMediumsolve
- 63Unique Paths IIMediumsolve
- 97Interleaving StringMediumsolve
- 72Edit DistanceMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.