Solution with O(1) space


  • 0
    L
    function minimumTotal(triangle) {
        for (var i = 1; i < triangle.length; i++) {
            for (var j = 0; j < triangle[i].length; j++) {
                var l = triangle[i - 1][j - 1] === undefined ? Number.MAX_VALUE: triangle[i - 1][j - 1];
                var r = triangle[i - 1][j] === undefined ? Number.MAX_VALUE: triangle[i - 1][j];
                triangle[i][j] += Math.min(l, r);
            }
        }
        return Math.min(...triangle.slice(-1)[0]);
    }

Log in to reply
 

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