JavaScript solution to Triangle with no extra space


  • 0
    O
    /**
    * @param {number[][]} triangle
    * @return {number}
    */
    var minimumTotal = function(triangle) {
        for (var i = triangle.length-2;i>=0;i--){
            var prev_floor = triangle[i];
            var next_floor = triangle[i+1];
            for (var j = 0;j<prev_floor.length;j++){
                if (next_floor[j] > next_floor[j+1]){
                    prev_floor[j] = prev_floor[j]+next_floor[j+1];
                }else{
                    prev_floor[j] = prev_floor[j]+next_floor[j];
                }
            }
        }
        return triangle[0][0];
    };

Log in to reply
 

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