You climb a staircase of n steps, taking 1 or 2 steps at a time. In how many distinct ways can you reach the top?
Only the previous two values matter, so keep just those.
1function climbStairs(n: number): number {2let a = 1, b = 1;3for (let i = 2; i <= n; i++) {4[a, b] = [b, a + b];5}6return b;7}
a = ways(i−2) = 1, b = ways(i−1) = 1.
Space: play/pause · ←/→: step