C# DP Solution


  • 0
    R

    C# DP Solution:

    public class Solution {
        int [,] cache = new int[1,1];
        
        public int UniquePaths(int m, int n) 
        {
            cache = new int[m,n];
            for(int i=0; i<cache.GetUpperBound(0); i++)
                for(int j = 0; j<cache.GetUpperBound(1); j++)
                    cache[i,j] = 0;
            
            return UniquePath(m, n, cache);
        }
        
        public int UniquePath(int m, int n, int[,] way) 
        {
            if(m==1||n==1)
                return 1; 
            
            int ways = 0;
            if(way[m-1,n-1]==0)
                {
                    way[m-1,n-1] = UniquePath(m-1,n, way) + UniquePath(m, n-1, way);
                    ways = way[m-1,n-1];
                }
            else 
                ways = way[m-1, n-1];
            return ways;
        }
        
    }
    

Log in to reply
 

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