LC 63. Unique Paths II

Nilanjan Deb · August 18, 2020

This is my CPP solution.

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>&grid) {
        int m = grid.size(), n = grid[0].size();
        long long int a[m][n];
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                a[i][j] = grid[i][j];
        if(a[0][0] == 1){
            return 0;
        }else{
            a[0][0] = 1;
        }
        for(int j=1;j<n;j++){
            if(a[0][j] == 0)
                a[0][j] = a[0][j-1];
            else
                a[0][j] = 0;
        }
        for(int i=1;i<m;i++){
            if(a[i][0] == 0)
                a[i][0] = a[i-1][0];
            else
                a[i][0] = 0;
        }
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                if(a[i][j] == 0){
                    a[i][j] = a[i][j-1] + a[i-1][j];
                }
                else{
                    a[i][j] = 0;
                }
            }
        }
        return a[m-1][n-1];
    }
};


Dicussion Forum