Fibonacci Number (Dynamic Programming)

Easy Dynamic Programming 1D DP Original on LeetCode

Fibonacci Number is a easy dynamic programming problem solved with the 1d dp pattern. The best approach, optimal (two variables), runs in O(n) time and O(1) space. Below are 3 approaches in Java, Python, C++ and JavaScript, from recursion (exponential) up.

Problem

Return the n-th Fibonacci number (F(0) = 0, F(1) = 1, F(n) = F(n - 1) + F(n - 2)). In the DP module, the goal is the path recursion → memoisation → tabulation → constant space.

Examples

Example 1

Input
n = 7
Output
13

Example 2

Input
n = 0
Output
0

Constraints

  • 0 <= n <= 30

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Recursion (exponential)O(2ⁿ)O(n)
Tabulation (bottom-up DP)O(n)O(n)
Optimal (two variables)O(n)O(1)

1Recursion (exponential)

TimeO(2ⁿ)
SpaceO(n)

fib(n) = fib(n - 1) + fib(n - 2). The same subproblems are solved over and over.

  1. if n < 2 return n; return fib(n - 1) + fib(n - 2).
class Solution {
    public int fib(int n) {
        return n < 2 ? n : fib(n - 1) + fib(n - 2);
    }
}

2Tabulation (bottom-up DP)

TimeO(n)
SpaceO(n)

Fill dp[0..n] from small to large. Each value is computed once, from values already in the table.

  1. dp[0] = 0, dp[1] = 1.
  2. dp[i] = dp[i - 1] + dp[i - 2].
class Solution {
    public int fib(int n) {
        if (n < 2) return n;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        for (int i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2];
        return dp[n];
    }
}

3Optimal (two variables)

TimeO(n)
SpaceO(1)

Keep only the last two values and roll them forward. This space optimisation applies to every DP whose state looks back a fixed number of steps.

  1. a = 0, b = 1; repeat n times: (a, b) = (b, a + b).
class Solution {
    public int fib(int n) {
        int a = 0, b = 1;
        for (int i = 0; i < n; i++) {
            int c = a + b;
            a = b;
            b = c;
        }
        return a;
    }
}

Edge cases to test

  • n = 0 and n = 1

Hints

Hint 1

dp[i] depends only on dp[i - 1] and dp[i - 2], so you only need two variables.

FAQ

What is the best time complexity for Fibonacci Number?

Optimal (two variables) runs in O(n) time and O(1) extra space.

Which pattern does Fibonacci Number use?

It is a dynamic programming problem that uses the 1d dp pattern. Other problems with the same pattern: Climbing Stairs, Min Cost Climbing Stairs (2 jumps), Minimal Cost (k jumps).

Is there a brute force solution for Fibonacci Number?

Yes. Recursion (exponential) takes O(2ⁿ) time and O(n) space. fib(n) = fib(n - 1) + fib(n - 2).

Which edge cases should I test for Fibonacci Number?

n = 0 and n = 1.