力扣(LeetCode) 120.三角形最小路径和(java)

题目

方法一:动态规划

public class MinimumTotal {

    public int minimumTotal(List<List<Integer>> triangle) {

        //动态规划,自上而下求出每一步的最小路径。
        int h = triangle.size();
        int[][] arr = new int[h][triangle.get(h-1).size()];
        arr[0][0] = triangle.get(0).get(0);
        for (int i = 1; i < h; i++) {
            List<Integer> l = triangle.get(i);
            for (int j = 0; j < l.size(); j++) {
                if (j == 0) {
                    arr[i][j] = arr[i-1][j] + l.get(j);
                } else if (j == (l.size()-1)){
                    arr[i][j] = arr[i-1][j-1] + l.get(j);
                } else {
                    arr[i][j] = Math.min(arr[i-1][j], arr[i-1][j-1]) + l.get(j);
                }
            }
        }
        int min = arr[h-1][0];
        for (int i = 1; i < triangle.get(h-1).size(); i++) {
            min = Math.min(min, arr[h-1][i]);
        }
        return min;
    }

    public static void main(String[] args) {
        MinimumTotal minimumTotal = new MinimumTotal();
        List<List<Integer>> triangle = new ArrayList<List<Integer>>();
        List<Integer> l = getList(new int[]{2});
        triangle.add(l);
        l = getList(new int[]{3,4});
        triangle.add(l);
        l = getList(new int[]{6,5,7});
        triangle.add(l);
        l = getList(new int[]{4,1,8,3});
        triangle.add(l);
        /*List<Integer> l = getList(new int[]{-10});
        triangle.add(l);*/
        System.out.println(minimumTotal.minimumTotal(triangle));
    }

    public static List<Integer> getList(int[] k) {
        List<Integer> l = new ArrayList<Integer>();
        for (int i = 0; i < k.length; i++) {
            l.add(k[i]);
        }
        return l;
    }

}

 方法二:动态规划(空间复杂度改进)

public class MinimumTotal {

  
    public static void main(String[] args) {
        MinimumTotal minimumTotal = new MinimumTotal();
        List<List<Integer>> triangle = new ArrayList<List<Integer>>();
        List<Integer> l = getList(new int[]{2});
        triangle.add(l);
        l = getList(new int[]{3,4});
        triangle.add(l);
        l = getList(new int[]{6,5,7});
        triangle.add(l);
        l = getList(new int[]{4,1,8,3});
        triangle.add(l);
        /*List<Integer> l = getList(new int[]{-10});
        triangle.add(l);*/
        System.out.println(minimumTotal.minimumTotal2(triangle));
    }

    int min = Integer.MAX_VALUE;

    //此方法最优
    public int minimumTotal2(List<List<Integer>> triangle) {

        //排除特殊情况
        if (triangle.size() == 1) {
            return triangle.get(0).get(0);
        }
        //要处理的第几行,上一次的结果,全部数据
        //相较于上一种减少了空间复杂度
        deal(1, new int[]{triangle.get(0).get(0)}, triangle);
        return min;
    }

    public void deal(int i, int[] pre, List<List<Integer>> triangle) {

        //全部处理完之后找出最小值
        if (i==triangle.size()) {
            for (int p : pre) {
                min = Math.min(min, p);
            }
            return;
        }
        //定义本次执行的结果数组
        int[] dp = new int[triangle.get(i).size()];
        List<Integer> l = triangle.get(i);
        for (int j = 0; j < l.size(); j++) {
            if (j == 0) {//最左边
                dp[j] = pre[j] + l.get(j);
            } else if (j == (l.size()-1)){//最右边
                dp[j] = pre[j-1] + l.get(j);
            } else {//中间
                dp[j] = Math.min(pre[j], pre[j-1]) + l.get(j);
            }
        }
        //继续递归
        deal(i+1, dp, triangle);
    }

}

LeetCode测试结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值