给定一个矩阵m,从左上角开始每次只能向右或者向下走,最后到达右下角的位置,路径上所有的数字累加起来就是路径和,返回所有路径和中最小的路径和

 

非常明显的动态规划题目,一目了然

package TT;

public class Test201 {

	public int minPathSum1(int[][] m){
		if(m==null || m.length==0 ||m[0]==null || m[0].length==0){
			return 0;
		}
		int row = m.length;
		int col = m[0].length;
		int[][] dp = new int[row][col];
		dp[0][0]=m[0][0];
		for(int i=1; i<row; i++){
			dp[i][0]=dp[i-1][0]+m[i][0];
		}
		for(int j =1; j<col; j++){
			dp[0][j]=dp[0][j-1]+m[0][j];
		}
		for(int i =1; i<col;i++){
			for(int j =1; j<row; j++){
				dp[i][j]=Math.min(dp[i-1][j], dp[i][j-1])+m[i][j];
			}
		}
		return dp[row-1][col-1];
	}

}

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-06-14
相关资源
相似解决方案