Minimum Path Sum
You are given an m × n grid of non-negative integers, where grid[r][c] is the cost of stepping on cell (r, c).
Start on the top-left cell and finish on the bottom-right cell, moving only right or down one cell at a time. The cost of a path is the sum of every cell it visits, including the first and the last. Return the smallest possible cost.
Constraints
1 <= m, n <= 2000 <= grid[r][c] <= 100
Examples
Example 1
Input: grid = [[1,4,2],[3,8,1],[5,2,6]] Output: 14
Right, right, down, down: 1 + 4 + 2 + 1 + 6 = 14. Always stepping to the cheaper neighbour (1, 3, 5, 2, 6) costs 17.
Example 2
Input: grid = [[2,1,5,1],[4,9,1,2],[7,1,1,3]] Output: 13
2 → 1 → 5 → 1 → 1 → 3: right, right, down, down, right.
Example 3
Input: grid = [[5]] Output: 5
A single cell is both the start and the end, and its cost counts.
JavaScript you'll use: Arrays: create, read, update Gotchas and debugging
/**
* @param {number[][]} grid
* @return {number}
*/
function minPathSum(grid) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.