Bottom Up 5 line C++ Solution


  • 27
    R
    class Solution {
    public:
        int minimumTotal(vector<vector<int> > &triangle) 
        {
            vector<int> mini = triangle[triangle.size()-1];
            for ( int i = triangle.size() - 2; i>= 0 ; --i )
                for ( int j = 0; j < triangle[i].size() ; ++ j )
                    mini[j] = triangle[i][j] + min(mini[j],mini[j+1]);
            return mini[0];
        }
    };

  • 0
    D

    4 lines
    class Solution {
    public:
    int minimumTotal(vector<vector<int>>& triangle) {
    for (int row = triangle.size() - 2; row >= 0; row--){
    for(int i = 0; i < triangle[row].size();i++){
    triangle[row][i]+=min(triangle[row + 1][i], triangle[row + 1][i + 1]);
    }
    }
    return triangle[0][0];
    }
    };


Log in to reply
 

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