getRow in constant space with JavaScript


  • 0
    N
    /**
     * @param {number} rowIndex
     * @return {number[]}
     */
    // const space solution
    var getRow = function(rowIndex) {
        const matrix = {};
        
        for(let i = 0; i <= rowIndex; i += 1) {
            matrix.response = Array(i + 1).fill(1);
            for(let j = 1; j < i; j += 1) {
                matrix.response[j] = matrix.previous[j - 1] + matrix.previous[j];
            }
            matrix.previous = matrix.response;
        }
        
        return matrix.response;
    };
    

Log in to reply
 

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