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 classSolution { Public intMinpathsum (int[] grid) { //Dynamic Programming Idea: Constructs a dp[][] two-dimensional array, Dp[i][j] represents the minimum and the number of rows that reach the J column of I,//when I>0 and J>0, Dp[i][j]=math.min (Dp[i][j-1],dp[i-1][j]) +grid[i][j]//note the processing of the first row and the first column//There is a practice that does not require additional space to modify the value directly on the original array introw=grid.length; intCol=grid[0].length; intdp[][]=New int[Row][col]; for(inti=0;i<row;i++){ if(i==0) dp[i][0]=grid[i][0]; ElseDp[i][0]=grid[i][0]+dp[i-1][0]; for(intj=1;j<col;j++){ if(i==0) {Dp[i][j]=dp[i][j-1]+Grid[i][j]; }Else{Dp[i][j]=math.min (Dp[i][j-1],dp[i-1][j]) +Grid[i][j]; } } } returnDp[row-1][col-1]; }}
[Leedcode 64] Minimum Path Sum