首页 > 其他 > 详细

leetcode120 - Triangle - medium

时间:2020-08-10 15:03:35      阅读:75      评论:0      收藏: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).

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.

 
最后到8的min path,一定经过5,因为5<7, 同样的,最后到1的min path也一定经过5,因为5<6。为了避免像到5的路径这样被重复计算,用dp-通过某些细分来一次性解决一类问题(重复子问题)
dp[i][j]表示从(0,0)到(i,j) 的min path sum。dp[0][0] = trangle[0][0]. 每一行i,最多i个数。对于坐标可以看成,当前位置(i,j)要么从左上方(i-1,j-1)来要么从正上方(i-1,j)来。边界条件最左边那排只能从正上方来,最右边那排只能从左上方来。
 
优化:dp数组可以降维,从top-down改成从botton-up来解。把最后一行看成是起点,那上面的某行某个node的min path sum就是它本身加上它两个children里小的那个(从小的那个方向来)。那就一行行往上算就好了,dp就存当前这行的结果(dp[j] = min(dp[j], dp[j+1])+triangle[i][j],RHS里的dp还是下面那行的,然后逐渐被替换成现在这行的),边界条件都省去了,最后结果就存在三角形顶端。
 
实现:
class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        
        int n = triangle.size();
        vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
        dp[0][0] = triangle[0][0];
        
        for (int i=1; i<n; i++){
            for (int j=0; j<=i; j++){
                if (j == 0)
                    dp[i][j] = dp[i-1][j] + triangle[i][j];
                else if ( j == i)
                    dp[i][j] = dp[i-1][j-1] + triangle[i][j];
                else
                    dp[i][j] = min(dp[i-1][j-1]+triangle[i][j], dp[i-1][j]+triangle[i][j]);
            }
        }
        
        return *min_element(dp[n-1].begin(), dp[n-1].end());
        
    }
};

 

优化:

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        
        int n = triangle.size();
        vector<int> dp(triangle.back());
        
        for (int i=n-2; i>=0; i--){
            for (int j=0; j<=i; j++){
                dp[j] = min(dp[j], dp[j+1])+triangle[i][j]; 
            }
        }

        return dp[0];
        
    }
};

 

leetcode120 - Triangle - medium

原文:https://www.cnblogs.com/xuningwang/p/13469219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!