The recursion that repeats itself
transcript
A fitness app has a stair challenge. A staircase has four stairs. A user climbs one or two stairs at a time. How many different ways are there? Recursion answers. But watch the calls.
The last move was one stair or two stairs. So ways of four is ways of three plus ways of two. Ways of zero and ways of one are one each. Every other call makes two more calls.
Follow the left side down to the base cases. Back at three, ways of one comes up a second time. Ways of two comes up again, with all its work. Nine calls. Four of them repeat an earlier call. Only five different questions exist.
Four stairs need nine calls. Forty stairs need over three hundred million calls. Yet only forty one different questions exist. Almost all of that work is repeated work.
Save each answer the first time. Read it when the same call comes again. Then each question is worked out once. That is called memoisation.
Repeated calls are the signal. Save the answers, and the work shrinks. Full breakdown in the lecture.