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.
public class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int column = grid[0].length;
int[][] result = new int[row][column];
for(int i = row - 1; i >= 0; i--) {
for(int j = column - 1; j >= 0; j--) {
if(i == row - 1 && j == column - 1) {
result[i][j] = grid[i][j];
}
else if(i == row - 1) {
result[i][j] = result[i][j+1] + grid[i][j];
}
else if(j == column - 1) {
result[i][j] = result[i+1][j] + grid[i][j];
}
else {
int min = Math.min(result[i+1][j], result[i][j+1]);
result[i][j] = min + grid[i][j];
}
}
}
return result[0][0];
}
}