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.
Or the extension of the previous question, to find the minimum path. Can still be extended on previous versions. In fact, are all 62 variants, understand DP, in fact, it is not difficult.
Public classSolution { Public intMinpathsum (int[] grid) { intm =grid.length; intn = grid[0].length; if(m = = 0 | | n = = 0) return0; intresult = 0; if(M = = 1){ for(inti = 0;i<n;i++) Result+=grid[0][i]; returnresult; } if(n = = 1){ for(inti = 0;i<m;i++) {result+ = Grid[i][0]; } returnresult; } int[] DP =New int[n]; dp[0] = grid[0][0]; for(inti = 1; i<n;i++) {Dp[i]= Dp[i-1]+grid[0][i]; } for(inti = 1;i<m;i++) {dp[0] = dp[0]+grid[i][0]; for(intj = 1;j<n;j++) {Dp[j]= Dp[j]>dp[j-1]?dp[j-1]+grid[i][j]:d p[j]+Grid[i][j]; } } returnDp[n-1]; }}
Leetcode Minimum Path Sum-----java