sort c# DP solution


  • 0
    T
        Dictionary<int,Dictionary<int,int>> mapping = new Dictionary<int,Dictionary<int,int>>();
        int mod = 1000000007;
        public int FindPaths(int m, int n, int N, int i, int j) {
            if(i < 0 || j < 0 || i >=m || j >= n)
                return 1;
            else if(N <= 0)
                return 0;
            else if(mapping.ContainsKey(i*n+j) && mapping[i*n+j].ContainsKey(N)){
                return mapping[i*n+j][N];
            }
            
            if(!mapping.ContainsKey(i*n+j))
                mapping.Add(i*n+j,new Dictionary<int,int>());
            
            int r = 0;
            r += FindPaths(m,n,N-1,i-1,j)%mod;
            r %=mod;
            r += FindPaths(m,n,N-1,i+1,j)%mod;
            r %=mod;
            r += FindPaths(m,n,N-1,i,j+1)%mod;
            r %=mod;
            r += FindPaths(m,n,N-1,i,j-1)%mod;
            r %=mod;
            
           mapping[i*n+j].Add(N,r);
           
           return r;
        }
        
    }
    
    

Log in to reply
 

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