美文网首页
120. Triangle

120. Triangle

作者: lqsss | 来源:发表于2018-03-27 15:20 被阅读0次

    题目

    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).
    

    思路

    动态规划的题目。

    1. 递归
    2. 二维数组保存dp[i][j]:到(i,j)位置时的最小值
    3. 自底向上一维数组 dp[i] = i索引处的最小值

    代码

    • 递归
     public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
                 int sum;
                 sum = getResult(triangle, 0, 0);
                 return sum;
               }
    
           
    
        public int getResult(ArrayList<ArrayList<Integer>> triangle, int l, int k) {
                 int sum = triangle.get(l).get(k);
                 if (l < triangle.size() - 1)
                    sum = sum + Math.min(getResult(triangle, l + 1, k), getResult(triangle, l + 1, k + 1));
                 return sum;
    
    • 从上至下
    public int minimumTotal(List<List<Integer>> triangle) {
            if (triangle.size() == 0) {
                return 0;
            }
            int[][] dp = new int[triangle.size()][triangle.get(triangle.size() - 1).size()];
            //dp[i][j]:在(i,j)的最小路径
            dp[0][0] = triangle.get(0).get(0);
            for (int i = 1; i < triangle.size(); i++) {
                for (int j = 0; j < triangle.get(i).size(); j++) {
                    if (j == 0) {
                        dp[i][j] = dp[i - 1][0] + triangle.get(i).get(j);
                    } else if (j == triangle.get(i).size() - 1) {
                        dp[i][j] = dp[i - 1][j - 1] + triangle.get(i).get(j);
                    } else {
                        dp[i][j] = triangle.get(i).get(j) + Math.min(dp[i - 1][j], dp[i - 1][j - 1]);
                    }
                }
            }
    
            int res = Integer.MAX_VALUE;
            //在dp最后一行比较一个最小路径
            for (int i = 0; i < dp[dp.length - 1].length; i++) {
                res = Math.min(res, dp[dp.length - 1][i]);
            }
            return res;
        }
    
    • 自底向上
        public int minimumTotal(List<List<Integer>> triangle) {
            if (triangle.size() == 0) {
                return 0;
            }
            int[] dp = new int[triangle.size()];
            for (int i = 0; i < dp.length; i++) {
                dp[i] = triangle.get(triangle.size() - 1).get(i);
            }
            //自底向上
            //dp[i]:到索引i的最小路径 dp[i] = arr[i] + min(dp[i+1] ,dp[i])
            //得到dp[0]
            for (int i = dp.length - 2; i >= 0; i--) {
                List<Integer> row = triangle.get(i);
                for (int j = 0; j < row.size(); j++) {
                    dp[j] = row.get(j) + Math.min(dp[j], dp[j + 1]);
                }
            }
            return dp[0];
        }
    

    相关文章

      网友评论

          本文标题:120. Triangle

          本文链接:https://www.haomeiwen.com/subject/rkvjcftx.html