Dynamic Programming Fibonacci algorithm

后端 未结 3 873
孤街浪徒
孤街浪徒 2021-01-25 02:40

I\'m working on an algorithm to calculate a Fibonacci number and got the pseudo code for it but I can\'t figure out how much time it takes to run. I think it runs at O(n) but no

3条回答
  •  一整个雨季
    2021-01-25 03:23

    Iterative solution to find the nth fibonnaci takes O(n) in terms of the value of n and O(2^length(n)) in terms of the size of n ( length(n) == number of bits to represent n). This kind of running time is called Pseudo-polynomial. However, if recursive method is used to find the fib of n, then it will take exponential time in terms of the value(O(2^n)). But this can be reduced by using dynamic programming approach to solve the fib of n.

提交回复
热议问题