short recursive C# solution using memoization (DP)


  • 0
    K
    public class Solution {
        public int UniquePaths(int m, int n) {
            int[,] grid = new int[m,n];
            return NumPaths(m-1,n-1,grid);
        }
        public int NumPaths(int x,int y,int[,] grid) {
            if (x == 0 || y == 0) return 1;
            grid[x,y] = (grid[x-1,y] == 0 ? NumPaths(x-1,y,grid) : grid[x-1,y]) + (grid[x,y-1] == 0 ? NumPaths(x,y-1,grid) : grid[x,y-1]);
            return grid[x,y];
        }
    }
    

Log in to reply
 

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