leetcode120. Triangle

120. Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle
[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

解法一

自底向上,动态规划

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return -1;
        }
        if (triangle.get(0) == null || triangle.get(0).size() == 0) {
            return -1;
        }

        // state: f.get(x).get(y) = minimum path value from x,y to bottom
        int len = triangle.size();
        // initail a new list
        List<List<Integer>> minSum = new ArrayList<>();
        List<Integer> temp = nullfor (int i = 0; i < len; i++) {
            temp = new ArrayList<>();
            for (int j = 0; j < triangle.get(i).size(); j++) {
                temp.add(j, 0);
            }
            minSum.add(i, temp);
        }
        // initial last row
        for (int j = 0; j < triangle.get(len - 1).size(); j++) {
            minSum.get(len - 1).set(j, triangle.get(len - 1).get(j));
        }
        // bottom up
        for (int i = len - 2; i >= 0; i--) {
            for (int j = 0; j < minSum.get(i).size(); j++) {
                minSum.get(i).set(j, Math.min(minSum.get(i + 1).get(j), minSum.get(i + 1).get(j + 1))
                + triangle.get(i).get(j));
            }
        }

        return minSum.get(0).get(0);
    }
}

这里写图片描述

解法二

直接在原list中进行变化。

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return -1;
        }
        if (triangle.get(0) == null || triangle.get(0).size() == 0) {
            return -1;
        }

        // state: f.get(x).get(y) = minimum path value from x,y to bottom
        int len = triangle.size();
        // bottom up
        for (int i = len - 2; i >= 0; i--) {
            for (int j = 0; j <= i; j++) {
                triangle.get(i).set(j, Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1))
                        + triangle.get(i).get(j));
            }
        }

        return triangle.get(0).get(0);
    }
}

这里写图片描述

解法三

up bottom,新初始化一个列表。

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return -1;
        }
        if (triangle.get(0) == null || triangle.get(0).size() == 0) {
            return -1;
        }

        // state: f.get(x).get(y) = minimum path value from x,y to bottom
        // initial new list
        int len = triangle.size();
        List<List<Integer>> minSum = new ArrayList<>();
        List<Integer> temp = null;
        for (int i = 0; i < len; i++) {
            temp = new ArrayList<>();
            for (int j = 0; j < triangle.get(i).size(); j++) {
                temp.add(j, 0);
            }
            minSum.add(i, temp);
        }

        // initial
        minSum.get(0).set(0, triangle.get(0).get(0));
        for (int i = 1; i < len; i++) {
            minSum.get(i).set(0, minSum.get(i - 1).get(0) + triangle.get(i).get(0));
            minSum.get(i).set(i, minSum.get(i - 1).get(i - 1) + triangle.get(i).get(i));
        }
        // up bottom
        for (int i = 1; i < len; i++) {
            for (int j = 1; j < i; j++) {
                minSum.get(i).set(j, Math.min(minSum.get(i - 1).get(j - 1), minSum.get(i - 1).get(j))
                        + triangle.get(i).get(j));
            }
        }

        int best = minSum.get(len - 1).get(0);
        for (int i = 0; i < len; i++) {
            if (minSum.get(len - 1).get(i) < best)
                best = minSum.get(len - 1).get(i);

        }
        return best;
    }
}

这里写图片描述

解法四

up bottom基于原列表list

    public int minimumTotal(List<List<Integer>> triangle) {
         if (triangle == null || triangle.size() == 0) {
            return -1;
        }
        if (triangle.get(0) == null || triangle.get(0).size() == 0) {
            return -1;
        }

        // state: f.get(x).get(y) = minimum path value from 0,0 to x,y
        int len = triangle.size();
        // up bottom
        for (int i = 1; i < len; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0) {
                    triangle.get(i).set(j, triangle.get(i - 1).get(j)
                            + triangle.get(i).get(j));
                } else if (j == i) {
                    triangle.get(i).set(j, triangle.get(i - 1).get(j - 1)
                            + triangle.get(i).get(j));
                } else {
                    triangle.get(i).set(j, Math.min(triangle.get(i - 1).get(j - 1), triangle.get(i - 1).get(j))
                            + triangle.get(i).get(j));
                }
            }
        }

        int best = triangle.get(len - 1).get(0);
        for (int i = 0; i < len; i++) {
            if (triangle.get(len - 1).get(i) < best)
                best = triangle.get(len - 1).get(i);

        }
        return best;
    }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值