63

faster than 100%
在前一题动态规划的基础上,做一个判决,遇到障碍时将该行该列以后的dp[i][j]都置0
首先对第一行第一列做判决,后面的可以根据前面的得到

/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function(obstacleGrid) {
    var m = obstacleGrid.length
    var n = obstacleGrid[0].length
    var dp = Array(m).fill().map(item => Array(n).fill(0))
    for(var i = 0; i < n; i++){
        if(obstacleGrid[0][i] === 0) dp[0][i] = 1
        else break
    }
    for(var i = 0; i < m; i++){
        if(obstacleGrid[i][0] === 0) dp[i][0] = 1
        else break
    }
    for(var i = 1; i < m; i++){
        for(var j = 1; j < n; j++){
            if(obstacleGrid[i][j] === 0) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        }
    }
    return dp[m - 1][n - 1]
};
};

你可能感兴趣的:(63)