So I have this memoized algorithm for the Fibonacci sequence and I faced something a little strange, there is no impact on the time complexity if I pass the memo dict in line 4, or if I don't, I want to understand why? isn't memo supposed to be empty in the later recursive calls?
here's the code without passing memo in line 4:
def fib(n , memo = {}):
if(n<=2): return 1
if(n in memo): return memo[n]
memo[n] = fib(n-1) + fib(n-2)
return memo[n]
and here it is when I pass it:
def fib(n , memo = {}):
if(n<=2): return 1
if(n in memo): return memo[n]
memo[n] = fib(n-1 , memo) + fib(n-2 , memo)
return memo[n]
you can try it there is no difference in the time complexity.