---
title: "Discussion on \"How to Solve the Staircase Problem with JavaScript using Memoization\" | Hashnode"
description: "Discussion on \"How to Solve the Staircase Problem with JavaScript using Memoization\". I want to write a quick follow up to my last post on How to Solve the Staircase Problem with 5 Lines of JavaScript.\nIt's a very elegant solution to the infamous staircase problem, however, as one commentor noted it's terribly inefficient to the order..."
url: "https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to Solve the Staircase Problem with JavaScript using Memoization](https://indepthjavascript.dev/how-to-solve-the-staircase-problem-with-javascript-using-memoization?utm_source=hashnode&utm_medium=feed)

I want to write a quick follow up to my last post on How to Solve the Staircase Problem with 5 Lines of JavaScript. It's a very elegant solution to the infamous staircase problem, however, as one commentor noted it's terribly inefficient to the order...

Jul 8, 2022 · 2 min read · indepthjavascript.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#recursion](https://hashnode.com/tag/recursion)
-   [#algorithms](https://hashnode.com/tag/algorithms)
-   [#memoization](https://hashnode.com/tag/memoization)

## Written by

[Amit Mehta](https://hashnode.com/@apmfree)

@apmfree · Abington, PA

  
[View profile](https://hashnode.com/@apmfree)

Javascript Developer who Loves to Code!

Self taught full stack developer who loves to code, just for fun. Enjoys health &amp; fitness, yoga, and meditation. Lives with his wonderful wife and 2 cats in Abington, PA.

## Responses 3

Markdown supported · ⌘↵ to post

[Jack Follansbee](https://hashnode.com/@MrPeach)

[Jul 23, 2022](https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a/comment/62db7a5dcf7a8a6d54f0605b)

Pardon me for rewriting in Python.

My code runs without recursion and at about the same speed.

```
def StairCaseMemo(N):
    memo = {}

    def stepsM(N):
        if N == 0: return 1
        elif N<0: return 0
        if N in memo.keys(): return memo[N]
        else:
            memo[N] = stepsM(N - 1) + stepsM(N - 2) + stepsM(N - 3);
            return memo[N];
    print(stepsM(N))

def StairCaseMine(limit):
    # initial setup
    array = {}
    array[0] = 1
    array[1] = array[2] = index = 0
    while index < limit:
        count = array[index]
        array[index+1] += count
        array[index+2] += count
        array[index+3] = count
        index += 1
    print(array[limit])
```

Enjoy!

[Juan Vidal Pich](https://hashnode.com/@juanvp)

[Jul 14, 2022](https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a/comment/62d04bf26b0f8eeb0e0671f3)

You could also do away with recursion altogether by applying dynamic programming. Start with an array of 3 values \[1,2,4\] for the possibilities of the first 3 steps and then calculate the following positions as the sum of the previous 3 positions in the array. The value at N - 1 is the number of possibilities.

[Jack Follansbee](https://hashnode.com/@MrPeach)

[Jul 23, 2022](https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a/comment/62d04bf26b0f8eeb0e0671f3#reply-62db8320cf7a8a6d54f061b7)

See above, that's fairly close to what my code does.

[Amr Saber](https://hashnode.com/@amr-saber)

[Jul 9, 2022](https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a/comment/62c8c7b2e62bf98e0fb06b25)

The order of the old implementation is not O(n^3) it's O(3^n) as you branch 3 times for each call, and you have total n different cases, so it's 3 \* 3 \* 3 \* 3 ... n times which is 3^n.

[Amit Mehta](https://hashnode.com/@apmfree)

[Jul 9, 2022](https://hashnode.com/posts/how-to-solve-the-staircase-problem-with-javascript-using-memoization/62c8bcbde62bf98e0fb06a4a/comment/62c8c7b2e62bf98e0fb06b25#reply-62c8da3abffc4694f5bc0efb)

Kudos for catching that! 👍
