number of paths

🏠

pramp question

 1 class Memoize:
 2     def __init__(self, fn):
 3         self.fn = fn
 4         self.memo = {}
 5     def __call__(self, *args):
 6         if args not in self.memo:
 7             self.memo[args] = self.fn(*args)
 8         return self.memo[args]
 9 
10 def num_of_paths_to_dest(n):
11   @Memoize
12   def dp(i, j):
13     if i == j == (n-1):
14       return 1
15     if i >= n or j >= n or (i > j):
16       return 0
17     return (0, dp(i+1, j))[i + 1 < n] + (0, dp(i, j+1))[j + 1 < n]
18   return dp(0, 0)