High interview relevanceRoadmap: Advanced
TimeNumber of states × transitions per state
SpaceDepends on memoization or tabulation

Why it costs this much

Each state is computed once and cached, so the total work is the number of states times the work to combine one state.

Code

from functools import lru_cache
@lru_cache(maxsize=None)
def climb(n):                           # states: n; transitions per state: 2
    if n <= 1:
        return 1
    return climb(n - 1) + climb(n - 2)  # O(n) total: each state computed once

Common pitfalls

Define the state before writing code. Memoization also uses stack depth, and tabulation can often keep only the previous row. Knapsack is pseudo-polynomial because it depends on the capacity value.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list