1 public class Solution { 2 public int minPathSum(int[][] grid) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int m = grid.length; 6 int n = grid[0].length; 7 int[][] result = new int[m][n]; 8 for(int i = 0; i < m; i++) 9 for(int j = 0; j < n; j++){ 10 if(i - 1 < 0 && j - 1 < 0) 11 result[0][0] = grid[0][0]; 12 else if(i - 1 < 0) 13 result[i][j] = result[i][j-1] + grid[i][j]; 14 else if(j - 1 < 0) 15 result[i][j] = result[i-1][j] + grid[i][j]; 16 else 17 result[i][j] = Math.min(result[i-1][j], result[i][j-1]) + grid[i][j]; 18 } 19 20 return result[m-1][n-1]; 21 } 22 }