Iterative dynamic programming for factorial works well but it violates the defination of dp as there are no overlapping sub problems in factorial
21:18 12 Feb 2020

I have a doubt regarding dynamic programming. According to the definition of dp, a problem can be solved using dp if it has optimal substructure and overlapping sub-problems. But in the factorial problem, all the subproblems are unique, but if we apply iterative dp still, we get the correct answer. Why does it happen??? Correct me if I am wrong

def fact(n):
  dp =  [-1 for i in range(n+1)]
  dp[0] = 1
  dp[1] = 1
  i = 2
  while i <= n:
    dp[i] = i*dp[i-1]
    i+=1
  return dp[n]
n = int(input())
ans = fact(n)
print(ans)
python data-structures