Concise C++ solution without extra spaces


  • 1
    E
    class Solution {
    public:
        int minimumTotal(vector<vector<int>>& triangle) {
            if (triangle.size() == 0) return 0;
            else if (triangle.size() == 1) return triangle[0][0];
    
            for (int i = triangle.size()-2; i >= 0; i--){
                for (int j = 0; j < triangle[i].size(); j++){
                    triangle[i][j] += min(triangle[i+1][j],triangle[i+1][j+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.