Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 509 Fibonacci - Problem Statement

LC 509 classic

Fibonacci sequence is an ideal example for recursion. To find fib(n)fib(n), you just call fib(n1)fib(n-1) and fib(n2)fib(n-2) and add them.

What could be simpler? I'll show you why this "obvious" solution breaks down when nn grows big. For small nn like 1010, it finishes instantly. For n=40n = 40, your computer struggles. For n=50n = 50, it never finishes. This problem will teach you why you need dynamic programming.