lattice path from project Euler with python solution

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I was trying to solve this problem called lattice path from Project Euler that "Count the number of unique paths to travel from the top left to the bottom right of a lattice of squares".
How many such routes are there through a 20ÃÂ20 grid?
It takes me forever to get an answer...
Image https://projecteuler.net/project/images/p015.gif
# Input: An interger N (which is the size of the lattice)
# Output: An interger (which represents the number of unique paths)
#
# Example: input: 2
#
# (2 x 2 lattice of squares)
# __ __
# |__|__|
# |__|__|
#
When moving through the lattice, you can only move either down or to the right.
Currently, I got 1 solution, and I am not sure how much better if we memoize it. But wanted to ask for code review for my solution below:
def lattice_paths(m, n):
if m == 0 and n == 0:
return 1
if m < 0 or n < 0:
return 0
return lattice_paths(m - 1, n) + lattice_paths(m, n - 1)
Here is the time complexity and space complexity of my first solution.
# Time Complexity: O(2^(M+N))
# Auxiliary Space Complexity: O(M+N)
dynamic approach with the memorized solution.
def lattice_paths(m, n): # m rows and n columns
def helper(x, y):
if x > helper.m or y > helper.n:
return 0
if helper.cache.get((x, y)) is not None:
return helper.cache[(x, y)]
helper.cache[(x, y)] = helper(x + 1, y) + helper(x, y + 1)
return helper.cache[(x, y)]
helper.cache =
helper.m = m
helper.n = n
helper(0,0)
if m == 0 and n == 0:
return 1
elif m < 0 or n < 0:
return 0
else:
return helper.cache[(0, 0)]
python
add a comment |Â
up vote
0
down vote
favorite
I was trying to solve this problem called lattice path from Project Euler that "Count the number of unique paths to travel from the top left to the bottom right of a lattice of squares".
How many such routes are there through a 20ÃÂ20 grid?
It takes me forever to get an answer...
Image https://projecteuler.net/project/images/p015.gif
# Input: An interger N (which is the size of the lattice)
# Output: An interger (which represents the number of unique paths)
#
# Example: input: 2
#
# (2 x 2 lattice of squares)
# __ __
# |__|__|
# |__|__|
#
When moving through the lattice, you can only move either down or to the right.
Currently, I got 1 solution, and I am not sure how much better if we memoize it. But wanted to ask for code review for my solution below:
def lattice_paths(m, n):
if m == 0 and n == 0:
return 1
if m < 0 or n < 0:
return 0
return lattice_paths(m - 1, n) + lattice_paths(m, n - 1)
Here is the time complexity and space complexity of my first solution.
# Time Complexity: O(2^(M+N))
# Auxiliary Space Complexity: O(M+N)
dynamic approach with the memorized solution.
def lattice_paths(m, n): # m rows and n columns
def helper(x, y):
if x > helper.m or y > helper.n:
return 0
if helper.cache.get((x, y)) is not None:
return helper.cache[(x, y)]
helper.cache[(x, y)] = helper(x + 1, y) + helper(x, y + 1)
return helper.cache[(x, y)]
helper.cache =
helper.m = m
helper.n = n
helper(0,0)
if m == 0 and n == 0:
return 1
elif m < 0 or n < 0:
return 0
else:
return helper.cache[(0, 0)]
python
1
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was trying to solve this problem called lattice path from Project Euler that "Count the number of unique paths to travel from the top left to the bottom right of a lattice of squares".
How many such routes are there through a 20ÃÂ20 grid?
It takes me forever to get an answer...
Image https://projecteuler.net/project/images/p015.gif
# Input: An interger N (which is the size of the lattice)
# Output: An interger (which represents the number of unique paths)
#
# Example: input: 2
#
# (2 x 2 lattice of squares)
# __ __
# |__|__|
# |__|__|
#
When moving through the lattice, you can only move either down or to the right.
Currently, I got 1 solution, and I am not sure how much better if we memoize it. But wanted to ask for code review for my solution below:
def lattice_paths(m, n):
if m == 0 and n == 0:
return 1
if m < 0 or n < 0:
return 0
return lattice_paths(m - 1, n) + lattice_paths(m, n - 1)
Here is the time complexity and space complexity of my first solution.
# Time Complexity: O(2^(M+N))
# Auxiliary Space Complexity: O(M+N)
dynamic approach with the memorized solution.
def lattice_paths(m, n): # m rows and n columns
def helper(x, y):
if x > helper.m or y > helper.n:
return 0
if helper.cache.get((x, y)) is not None:
return helper.cache[(x, y)]
helper.cache[(x, y)] = helper(x + 1, y) + helper(x, y + 1)
return helper.cache[(x, y)]
helper.cache =
helper.m = m
helper.n = n
helper(0,0)
if m == 0 and n == 0:
return 1
elif m < 0 or n < 0:
return 0
else:
return helper.cache[(0, 0)]
python
I was trying to solve this problem called lattice path from Project Euler that "Count the number of unique paths to travel from the top left to the bottom right of a lattice of squares".
How many such routes are there through a 20ÃÂ20 grid?
It takes me forever to get an answer...
Image https://projecteuler.net/project/images/p015.gif
# Input: An interger N (which is the size of the lattice)
# Output: An interger (which represents the number of unique paths)
#
# Example: input: 2
#
# (2 x 2 lattice of squares)
# __ __
# |__|__|
# |__|__|
#
When moving through the lattice, you can only move either down or to the right.
Currently, I got 1 solution, and I am not sure how much better if we memoize it. But wanted to ask for code review for my solution below:
def lattice_paths(m, n):
if m == 0 and n == 0:
return 1
if m < 0 or n < 0:
return 0
return lattice_paths(m - 1, n) + lattice_paths(m, n - 1)
Here is the time complexity and space complexity of my first solution.
# Time Complexity: O(2^(M+N))
# Auxiliary Space Complexity: O(M+N)
dynamic approach with the memorized solution.
def lattice_paths(m, n): # m rows and n columns
def helper(x, y):
if x > helper.m or y > helper.n:
return 0
if helper.cache.get((x, y)) is not None:
return helper.cache[(x, y)]
helper.cache[(x, y)] = helper(x + 1, y) + helper(x, y + 1)
return helper.cache[(x, y)]
helper.cache =
helper.m = m
helper.n = n
helper(0,0)
if m == 0 and n == 0:
return 1
elif m < 0 or n < 0:
return 0
else:
return helper.cache[(0, 0)]
python
edited Aug 2 at 21:05
asked Aug 2 at 20:03
NinjaG
634221
634221
1
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44
add a comment |Â
1
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44
1
1
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
here's a solution that uses cache to memoize the solution
def lattice_paths(m, n):
cache = [1]
larger = max(m, n)
smaller = min(m, n)
while(len(cache) < larger + 1):
for i in range(1, len(cache)):
cache[i] += cache[i - 1]
cache.append(2 * cache[len(cache) - 1])
return cache[smaller]
# Time Complexity: O(N)
# Auxiliary Space Complexity: O(N)
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
here's a solution that uses cache to memoize the solution
def lattice_paths(m, n):
cache = [1]
larger = max(m, n)
smaller = min(m, n)
while(len(cache) < larger + 1):
for i in range(1, len(cache)):
cache[i] += cache[i - 1]
cache.append(2 * cache[len(cache) - 1])
return cache[smaller]
# Time Complexity: O(N)
# Auxiliary Space Complexity: O(N)
add a comment |Â
up vote
0
down vote
accepted
here's a solution that uses cache to memoize the solution
def lattice_paths(m, n):
cache = [1]
larger = max(m, n)
smaller = min(m, n)
while(len(cache) < larger + 1):
for i in range(1, len(cache)):
cache[i] += cache[i - 1]
cache.append(2 * cache[len(cache) - 1])
return cache[smaller]
# Time Complexity: O(N)
# Auxiliary Space Complexity: O(N)
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
here's a solution that uses cache to memoize the solution
def lattice_paths(m, n):
cache = [1]
larger = max(m, n)
smaller = min(m, n)
while(len(cache) < larger + 1):
for i in range(1, len(cache)):
cache[i] += cache[i - 1]
cache.append(2 * cache[len(cache) - 1])
return cache[smaller]
# Time Complexity: O(N)
# Auxiliary Space Complexity: O(N)
here's a solution that uses cache to memoize the solution
def lattice_paths(m, n):
cache = [1]
larger = max(m, n)
smaller = min(m, n)
while(len(cache) < larger + 1):
for i in range(1, len(cache)):
cache[i] += cache[i - 1]
cache.append(2 * cache[len(cache) - 1])
return cache[smaller]
# Time Complexity: O(N)
# Auxiliary Space Complexity: O(N)
answered Aug 3 at 17:28
NinjaG
634221
634221
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200855%2flattice-path-from-project-euler-with-python-solution%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
That's more of a mathematics and algorithm problem than a coding one. Check out this excellent blog post
â Daniel Lenz
Aug 2 at 21:44