zoukankan      html  css  js  c++  java
  • LeetCode: Triangle 解题报告

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

    Note:
    Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

    SOLUTION 1:

    使用DFS 加记忆矩阵的解法.

    mem[i][j]表示第i行第j列的解。它的解可以由下一行推出:mem[i][j] = mem[i+1][j] + mem[i+1][j+1]

     1 /*
     2     REC, SOL 1:
     3     */
     4     public int minimumTotal1(List<List<Integer>> triangle) {
     5         if (triangle == null || triangle.size() == 0) {
     6             return 0;
     7         }
     8         
     9         int rows = triangle.size();
    10         int[][] mem = new int[rows][rows];
    11         for (int i = 0; i < rows; i++) {
    12             for (int j = 0; j < rows; j++) {
    13                 mem[i][j] = Integer.MAX_VALUE;
    14             }
    15         }
    16         
    17         return dfs(triangle, 0, 0, mem);
    18     }
    19     
    20     public int dfs(List<List<Integer>> triangle, int row, int col, int[][] mem) {
    21         if (mem[row][col] != Integer.MAX_VALUE) {
    22             return mem[row][col];
    23         }
    24         
    25         if (row == triangle.size() - 1) {
    26             mem[row][col] = triangle.get(row).get(col);
    27         } else {
    28             int left = dfs(triangle, row + 1, col, mem);
    29             int right = dfs(triangle, row + 1, col + 1, mem);    
    30             mem[row][col] = triangle.get(row).get(col) + Math.min(left, right);
    31         }
    32         
    33         return mem[row][col];
    34     }
    View Code

    SOLUTION 2:

    ref: http://blog.csdn.net/imabluefish/article/details/38656211

    动态规划的题目

    我们可以轻松将上面的修改为DP.

    并且,为了减少内存使用量,使用一维DP即可。

    f[j] 表示下一行第j列某点到最后底部的最短值。因为我们只需要下一行的这个值,所以我们使用一行的DP memory即可完成任务。

    第一步: 先计算出最后一排的最短值,实际上就是这一排本身的值。
    第二步:From bottom to up, 每一层的最短值只需要把自身值加上,并且取下层的左右邻接点的最小值。

     1 /*
     2     DP, SOL 2:
     3     */
     4     public int minimumTotal(List<List<Integer>> triangle) {
     5         if (triangle == null || triangle.size() == 0) {
     6             return 0;
     7         }
     8         
     9         int rows = triangle.size();
    10         int[] D = new int[rows];
    11         
    12         for (int i = rows - 1; i >= 0; i--) {
    13             // 注意:边界条件是 j <= i
    14             for (int j = 0; j <= i; j++) {
    15                 if (i == rows - 1) {
    16                     D[j] = triangle.get(i).get(j);
    17                 } else {
    18                     D[j] = triangle.get(i).get(j) + Math.min(D[j], D[j + 1]);
    19                 }    
    20             }
    21         }
    22         
    23         return D[0];
    24     }
    View Code

    GITHUB:

    https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dp/MinimumTotal.java

  • 相关阅读:
    VB6之GDI+加载PNG图片
    VB6之阴影图层
    VB6之截图
    VB6之调整任务栏按钮的位置
    恐怖的ifdown eth0;0
    VB6之WM_COPYDATA
    删除整个链表
    digest 词根 gest
    new和delete
    static, const
  • 原文地址:https://www.cnblogs.com/yuzhangcmu/p/4147764.html
Copyright © 2011-2022 走看看