Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
Created January 11, 2014 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaipakartik/8366140 to your computer and use it in GitHub Desktop.
Save kaipakartik/8366140 to your computer and use it in GitHub Desktop.
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20×20 grid?
short = dict()
def getPaths(x, y):
if x == 0 and y == 0:
return 0
if x == 0:
return 1
if y == 0:
return 1
if (x,y) in short:
return short[x,y]
# print x, y
short[x,y] =getPaths(x, y - 1) + getPaths(x-1, y)
return short[x,y]
print getPaths(20, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment