educative.io

Global dictionary for memoization don't work

Why Global dictionary for memoization doesn’t work?

Hi @Josimar_Amilcar_Fern!

A global dictionary should work as well, but only if the value for m remains constant across subsequent function calls.

If, for example, you use a global dictionary for the call staircase(4, 2), the memo will store the number of ways a staircase can be climbed if we can take a maximum of 2 steps. If we then call staircase(20, 3), the problem requires us to take a maximum of 3 steps, however, the dictionary has been created according to m = 2, and the result will be incorrect.

Hope this helps!

1 Like