【medium】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).

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.

 
从两个角度来解题:
(1)top bottom (memorization)
 
class Solution {
private:
    int minimum(vector<vector<int>>& triangle, vector<vector<int>>& dp, int i, int j){
     // 1.边界
if (i == triangle.size()-1){ dp[i][j] = triangle[i][j]; return triangle[i][j]; } // 2.如果cached if (dp[i][j] != INT_MAX) return dp[i][j]; // 3.递推公式 dp[i][j] = triangle[i][j] + min({minimum(triangle, dp, i+1, j), minimum(triangle, dp, i+1, j+1)}); return dp[i][j]; } public: int minimumTotal(vector<vector<int>>& triangle) { vector<vector<int>> dp; // 1.记忆数组
     //2.记忆数组初始化 dp.resize(triangle.size());
//r行 for (int k = 0; k < triangle.size(); k++){ dp[k].resize(triangle.size());//每行为c列 } for (int i=0; i<triangle.size(); i++){ for (int j=0; j<triangle.size(); j++) dp[i][j] = INT_MAX; }    //3. solution函数 minimum(triangle, dp, 0, 0); return dp[0][0]; } };

 

  (2) bottom up (tabulation) --- 更加简单直观
 
class Solution{
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        if (triangle.size() == 1)
            return triangle[0][0];
        
        vector<vector<int>> dp;
        dp.resize(triangle.size());//r行
        for (int k = 0; k < triangle.size(); k++){
           dp[k].resize(triangle.size());//每行为c列
        }
        
        for (int i=triangle.size()-1; i>=0; i--)
            dp[triangle.size()-1][i] = triangle[triangle.size()-1][i];
        
        for (int i=triangle.size()-2; i>=0; i--){
            for (int j=0; j<=i; j++)
                dp[i][j] = triangle[i][j] + min({dp[i+1][j], dp[i+1][j+1]});
        }
        
        return dp[0][0];
    }
};

 

 
posted @ 2019-08-30 11:10  Sherry_Yang  阅读(147)  评论(0编辑  收藏  举报