Created
May 21, 2021 16:17
-
-
Save rdisipio/53502a062923c73885f59cab9313d435 to your computer and use it in GitHub Desktop.
Unique paths using memoization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
m = 3 | |
n = 4 | |
S = [[None for _ in range(n+1)] for _ in range(m+1)] | |
def explore(i, j): | |
if i < 0 or j < 0 or i > m or j > n: | |
return 0 | |
if i==0 and j==0: | |
return 1 | |
if S[i][j] is not None: | |
return S[i][j] | |
S[i][j] = explore(i-1, j) + explore(i, j-1) | |
return S[i][j] | |
n_paths = explore(m-1, n-1) | |
print(f"Number of unique paths: {n_paths}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment