lattice path from project Euler with python solution

The name of the pictureThe name of the pictureThe name of the pictureClash 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)]






share|improve this question

















  • 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
















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)]






share|improve this question

















  • 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












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)]






share|improve this question













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)]








share|improve this question












share|improve this question




share|improve this question








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












  • 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










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)





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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)





    share|improve this answer

























      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)





      share|improve this answer























        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)





        share|improve this answer













        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)






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Aug 3 at 17:28









        NinjaG

        634221




        634221






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods