[Algorithm] Bottom-up Dynamic programming approch

Fibonacci sequence: 

The most well-known approach is memoization. Advantage is fast, but space complexity is big. O(N).

Bottom-up:

1 : Bottom-up dynamic programming represents recursive problems as Directed Acyclic Graph.

2. The DAG repressentation can be traversed in order of dependency.

3. Problems can be solved in minimal time and space.

F5: only depends on F3 and F4

 

Each step, we only need to keep two values in memory,  for example, caluclate F3, all we need is F1 and F2, we can dump F0.

def fib(n)
    a = 1
    b = 1
    for i in range(2, n+1):
        a, b = b, a +b

    return b

原文地址:https://www.cnblogs.com/Answer1215/p/14290984.html