DP solution cpp


  • 0
    A

    Here is the accpted solution in cpp with dp.

    class Solution {
    public:
        int uniquePathsWithObstacles(vector<vector<int>>& arr) {
            int n=arr.size();
            int m=arr[0].size();
            
            int dp[n][m];
            int i,j;
            for(i=0;i<n;i++)
            {
                if(arr[i][0]==1){
                    while(i<n){
                        dp[i][0]=0;
                        i++;
                    } 
                } 
                else
                    dp[i][0]=1;
            }
            
            for(i=0;i<m;i++)
            {
                if(arr[0][i]==1){
                    while(i<m){
                        dp[0][i]=0;
                        i++;
                    } 
                } 
                else
                    dp[0][i]=1;
            }
            
            for(i=1;i<n;i++){
                for(j=1;j<m;j++){
                    if(arr[i][j]==1)
                        dp[i][j]=0;
                    else
                        dp[i][j]=dp[i-1][j]+dp[i][j-1];
                }
            }
            
            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.