Dynamic Programming - Preamble
Preamble:
Fibonacci Series:
If n = 0: return 0;
If n<=2: return 1;
return f(n-1)+f(n-2)
Time Complexity:
It's the Fibonacci Recurrence function. T(n) = T(n-1) + T(n-2) + O(1) ~ phi^2 (phi is the golden ratio)
Otherwise we can check the lower bound
T(n) > 2T(n-2) ~ O(2^(n/2))
Correct algorithm but very bad.
Time Complexity: Exponential. Space Complexity, O(n) if consider call stack size, O(1) if not
Improvement:
Memoization (Memoized DP algorithm)
Sol 1. Time Complexity: O(n), Space Complexity, O(n)
Save already computed solution to dictionary(the memo), or save it in an array to save hash lookup time.
In this case, fib(k) only recurses the first time called for every k.
-memoized calls cost O(1)
-# of nonmemoized calls is n: fib(1), fib(2), ...fib(n)
-nonrecursive work per call is O(1)
So, overall, the time complexity for this solution is O(n)
Sol 2: Bottom-up DP algorithm, Space Optimized Method
just keep previous two numbers in the cache.
Time Complexity: O(n), Space Complexity: O(1)
Sol 3: There is a log(n) solution for this problem based on matrix multiplication.
Time Complexity: O(n), Space Complexity: O(1)
ref: http://www.geeksforgeeks.org/program-for-nth-fibonacci-number/

浙公网安备 33010602011771号