Sharing my C# solution


  • 0
    Y
    public class Solution {
        public int UniquePathsWithObstacles(int[,] obstacleGrid) {
            
            
            //DP
            
            var colums = obstacleGrid.GetLength(0);
            var rows = obstacleGrid.GetLength(1);
            var DP=new int[colums,rows];
            
            for(int i=0; i<colums; i++)
            {
                for(int j = 0; j < rows; j++)
                {
                    if(obstacleGrid[i,j]==1)
                    {
                        DP[i,j]=0;
                    }
                    else
                    {
                        if(i==0 && j==0)
                        {
                            DP[i,j]=1;
                        }
                        else if(i==0)
                        {
                            DP[i,j] = DP[i,j-1];
                        }
                        else if(j==0)
                        {
                            DP[i,j] = DP[i-1,j]; 
                        }
                        else
                        {
                            DP[i,j] = DP[i-1,j] + DP[i,j-1];
                        }
                    }
                }
                
            }
            
            return DP[colums-1,rows-1];
            
            //consider Obstacle
            
        }
    }

Log in to reply
 

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