Concise C++ Solution


  • 0
    I
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if (obstacleGrid.empty() || obstacleGrid[0].empty()) return 0;
        int m = obstacleGrid.size(), n = obstacleGrid[0].size();
        vector<int> paths(n, 0);
        paths[0] = 1;
        for (int i = 0; i < m; i++) {
            if (obstacleGrid[i][0]==1) paths[0] = 0;
            for (int j = 1; j < n; j++) {
               if (obstacleGrid[i][j]==1) paths[j] = 0;
                else paths[j] = paths[j] + paths[j-1];
            }
        }
        return paths[n-1];
    }
    

Log in to reply
 

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