当前位置:网站首页>leetcode 120. Triangle minimum path sum

leetcode 120. Triangle minimum path sum

2022-06-11 15:24:00 Amnesia machine

  Dynamic programming problem .dp Array , The equation is dp[j][i] = Math.min(dp[j+1][i],dp[j+1][i+1]) + At present value value .

java The code is as follows

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

原网站

版权声明
本文为[Amnesia machine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111516525292.html