Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of All numbers along its path.
Note:you can only move either down or right at the any.
Train of thought: This problem is very similar to the first few robot questions, but it changes a little, the specific code and comments as follows:
public class Solution {public
int minpathsum (int[][] grid) {
//Dynamic planning ideas
//min value (minimum from top and left) to this point for
( int i = 0; i < grid.length; i++)
for (int j = 0; J < Grid[0].length; j) {
if (i > 0 && J > 0)//Three case, second row after second column
Grid[i][j] + = Math.min (grid[i-1][j],grid[i][j-1]);
else if (i = = 0 && J > 0)//First line
grid[i][j] + = grid[i][j-1];
else if (i > 0 && j==0)//First column
grid[i][j] + = Grid[i-1][j];
}
Returns the last value return
grid[grid.length-1][grid[0].length-1];
}