My simple C++ solution, accepted for 4ms


  • 0
    Z
    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            int m=obstacleGrid.size();
            if(m<1) return 0;
            int n=obstacleGrid[0].size();
            if(n<1) return 0;
            
            vector<vector<int>> res=obstacleGrid;
            if(obstacleGrid[0][0]) return 0;
            res[0][0]=1;
            for(int j=1;j<n;j++)
            res[0][j]=obstacleGrid[0][j]?0:res[0][j-1];
            for(int i=1;i<m;i++)
            res[i][0]=obstacleGrid[i][0]?0:res[i-1][0];
            
            for(int i=1;i<m;i++)
            for(int j=1;j<n;j++)
            res[i][j]=obstacleGrid[i][j]?0:(res[i-1][j]+res[i][j-1]);
            
            return res[m-1][n-1];
        }
    };

Log in to reply
 

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