C++ 6 lines O(n) DP without modifying triangle


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.