Advertisement
Guest User

Untitled

a guest
May 6th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. public class Solution {
  2. public int minPathSum(int[][] grid) {
  3. int[][] dp = new int[grid.length][grid[0].length];
  4. //let dp[i][j] denotes the minimum sum from (0,0) to (i,j)
  5. dp[0][0]=grid[0][0];
  6. int m = grid.length, n = grid[0].length;
  7. //initilize the first row and column
  8. for (int row=1;row<grid.length;row++)
  9. dp[row][0]+=dp[row-1][0]+grid[row][0];
  10. for (int col=1;col<grid[0].length;col++)
  11. dp[0][col]+=dp[0][col-1]+grid[0][col];
  12.  
  13. for (int i=1;i<grid.length;i++)
  14. for(int j=1;j<grid[0].length;j++){
  15. dp[i][j]=grid[i][j]+Math.min(dp[i-1][j],dp[i][j-1]);
  16. }
  17.  
  18. return dp[m-1][n-1];
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement