Tensorum
LeetCode 509 · 1D / Fibonacci

Fibonacci Number

LeetCode 509 · Easy · solution, intuition & step-by-step walkthrough

F(i) only depends on F(i-1) and F(i-2), so two rolling variables are enough.

EasyMathDynamic ProgrammingRecursionMemoization
31 msruntime · beats 100%
19.2 MBmemory · beats 7%
3approaches, in order
11accepted runs · last 6 Apr 2026

This is how I solved Fibonacci Number (LeetCode 509), an easy problem from the 1D / Fibonacci pattern. Below: the intuition, the approach step by step, my accepted Python code and the 3 versions I wrote on the way, animated test cases and a line-by-line debugger.

Intuition

Each Fibonacci number is the sum of the two before it. A direct recursive definition works but recomputes the same values over and over; storing them turns an exponential call tree into a straight line.

Approach

  1. 1

    Handle the two seeds: F(0) = 0 and F(1) = 1.

  2. 2

    Recursive version: return fib(n-1) + fib(n-2) — correct but repeats work.

  3. 3

    Tabulation: fill dp[i] = dp[i-1] + dp[i-2] for i from 2 to n.

  4. 4

    Space-optimise: keep prev2 and prev, compute cur, then shift both forward.

How I solved it · from first idea to final

1
Plain recursionO(2ⁿ) time · O(n) stack

Straight from the definition — watch how often the same calls repeat.

2
TabulationO(n) time · O(n) space

Build the sequence in an array.

3
Two variablesAcceptedO(n) time · O(1) space

Roll prev2 → prev → cur forward.

Plain recursionPythonO(2ⁿ) time · O(n) stack
class Solution:
    def fib(self, n: int) -> int:
        if n==0:
            return 0
        if n==1:
            return 1
        return self.fib(n-1)+self.fib(n-2)
TabulationPythonO(n) time · O(n) space
class Solution:
    def fib(self, n: int) -> int:
        if n==0:
            return 0
        if n==1:
            return 1
        dp=[0]*(n+1)
        dp[0]=0
        dp[1]=1
        for i in range(2,n+1):
            dp[i]=dp[i-1]+dp[i-2]
        return dp[n]
Two variablesPythonO(n) time · O(1) space
class Solution:
    def fib(self, n: int) -> int:
        if n==0:
            return 0
        if n==1:
            return 1
        dp=[0]*(n+1)
        prev2=0
        prev=1
        for i in range(2,n+1):
            cur = prev+prev2
            #prev2 prev cur
            #     prev2 prev
            prev2=prev
            prev=cur
        return prev
Loading the step-through…

Fibonacci Number — FAQ

What is the intuition behind Fibonacci Number?

Each Fibonacci number is the sum of the two before it. A direct recursive definition works but recomputes the same values over and over; storing them turns an exponential call tree into a straight line.

How do you solve Fibonacci Number (LeetCode 509)?

1. Handle the two seeds: F(0) = 0 and F(1) = 1. 2. Recursive version: return fib(n-1) + fib(n-2) — correct but repeats work. 3. Tabulation: fill dp[i] = dp[i-1] + dp[i-2] for i from 2 to n. 4. Space-optimise: keep prev2 and prev, compute cur, then shift both forward.

What is the time and space complexity of Fibonacci Number?

Time: O(n). Space: O(1). (Accepted approach: Two variables.)

What are common mistakes in Fibonacci Number?

The naive recursion is O(2^n) — fine for n ≤ 30 here, but it is the classic reason to memoise. Return early for n = 0 and n = 1 before indexing dp[1].

Is there more than one way to solve Fibonacci Number?

Plain recursion (O(2ⁿ) time · O(n) stack): Straight from the definition — watch how often the same calls repeat. Tabulation (O(n) time · O(n) space): Build the sequence in an array. Two variables (O(n) time · O(1) space): Roll prev2 → prev → cur forward.