[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.

[Algorithm] Bottom-up Dynamic programming approch

F5: only depends on F3 and F4

[Algorithm] Bottom-up Dynamic programming approch 

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

 

[Algorithm] Bottom-up Dynamic programming approch

上一篇:0.1 + 0.2 != 0.3?


下一篇:免申请获得DataWorks生产环境表权限