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 any point in time.
解题思路:
和前面两个题的思路一样!
public int minPathSum(int[][] grid) {
if(grid == null) return 0;
int xlen = grid.length;
int ylen = grid[0].length;
int[][] arr = new int[xlen][ylen];
arr[xlen-1][ylen-1] = grid[xlen-1][ylen-1];
for(int i = xlen-2;i >= 0; i --){
arr[i][ylen-1] = grid[i][ylen-1]+arr[i+1][ylen-1];
}
for(int j = ylen-2; j >= 0; j --){
arr[xlen-1][j] = grid[xlen-1][j]+arr[xlen-1][j+1];
}
for(int i = xlen-2; i >= 0; i --){
for(int j = ylen-2; j >= 0; j--){
int min = arr[i+1][j]<=arr[i][j+1]?arr[i+1][j]:arr[i][j+1];
arr[i][j]=grid[i][j]+min;
}
}
return arr[0][0];
}