Clean c++ solution 4ms


  • 0
    A
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int n = obstacleGrid.size();
        int m = obstacleGrid[0].size();
        int DP[n][m];
        DP[0][0] = obstacleGrid[0][0] ? 0 : 1;
        for(int i=1;i<n;i++) {
            if(obstacleGrid[i][0] || !DP[i-1][0]) DP[i][0] = 0;
            else DP[i][0] = 1;
        }
        for(int i =1;i<m;i++) {
            if(obstacleGrid[0][i] || !DP[0][i-1]) DP[0][i] = 0;
            else DP[0][i] = 1;
        }
        for(int i=1;i<n;i++){
            for(int j=1;j<m;j++){
                if(!obstacleGrid[i][j]) DP[i][j] = DP[i][j-1] + DP[i-1][j];
                else DP[i][j] = 0;
            }
        }
        return DP[n-1][m-1];
        
    }

Log in to reply
 

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