cocobear9  
一枚普通的zisuer(lll¬ω¬),努力每天多学一点点

给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。

相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。

 

例如,给定三角形:

[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。

 

说明:

 

链接:https://leetcode-cn.com/problems/triangle

public int minimumTotal(List<List<Integer>> triangle) {
        int n = triangle.size();
        int [][]dp= new int[n+1][n+1];
        for(int i=n-1;i>=0;i--) {
            for(int j=0;j<=i;j++) {
                dp[i][j] =Math.min(dp[i+1][j], dp[i+1][j+1])+triangle.get(i).get(j);
                System.out.println("dp["+i+"]["+j+"]="+dp[i][j]);
            }
        }
        return dp[0][0];

    }

 

 

 

posted on 2020-07-15 23:45  cocobear9  阅读(103)  评论(0编辑  收藏  举报