Climbing Stairs

Transitions

Observe that for every stair, you can make two decisions:

  1. Climb one step

  2. Climb two steps

Recurrence tree

We don't count leaves that end with values where v > n. We can also memoize the results of sub-trees such as 2, reducing the computation of the right sub-tree to a O(1)O(1) lookup.

Top-down

Top-down can often be implemented by directly modelling the state transitions

The recurrence relation above looks like this:

dp(m)={1,m==n0,m>ndp(m+1)+dp(m+2)dp(m) = \begin{cases} 1, m == n\\ 0, m > n \\ dp(m + 1) + dp(m + 2) \end{cases}

Bottom-up

By re-framing the problem, we will notice that if we are index mm and we have computed the optimal solution for 0..m10..m-1 (prefix), then we can obtain the optimal solution for mm using the following recurrence:

dp(m)={1,m=12,m=2dp(m1)+dp(m2)dp(m) = \begin{cases} 1, m = 1\\ 2, m = 2\\ dp(m-1) + dp(m - 2) \end{cases}

Optimization

From the recurrence relation, we only ever require the previous 2 states, dp(m - 1) and dp(m - 2) to compute dp(m). So, we can store these 2 states as variables instead of using an array.

Keen observers will notice that this is basically the Fibonacci sequence

Last updated