DP Time Complexity O(m*n), Space O(n)


  • 0
    J
    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& o) {
            int m = o.size(), n = o[0].size();
            vector<int> dp(n,0); dp[0] = 1-o[0][0];
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(o[i][j] == 1) { dp[j] = 0; continue;}
                    dp[j] = dp[j] + dp[j-1];
                }
            }
            return dp[n-1];
        }
    };
    

Log in to reply
 

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