Is the Big O entry the same for memoized iteration recursion?

I use a simple head-top example here

function factorial(n)
   if n==1 return 1
   else return n*factorial(n-1)

function factorial(n)
    result = 1
    for i = 1 to n
        result *= n
    return result

Or functions that are recursive and have memoization against dynamic programming, where you iterate over an array and fill in values, etc.

I know that sometimes recursion is bad because you might run out of memory (tail recursion?) With a bunch (or stack?), But does any of them affect O notation?

Does a recursive memoized algorithm have the same O-notation / speed as an iterative version?

+4
source share
4 answers

.

, , , .

- O (n), - O (n) O (1) .

. , , , .

+1

, , . , dict Python ( () O(1) //), memoization (O(n)) .

, , . O(1), memoized O(n).

0

, , , . , . C- , .

0

? , ( ), memoizing - , - memoized values, , , - .

... , O (n) . .. factorial (10), 10 0 - O (1). (15), 15 * 14 * 13 * 12 * 11 * factorial (10), , 6 ( 15).

, , lookup dict . - factorial (10) 10, 0, . memoization dict.

0

All Articles