Codele / Algorithms
Dynamic programming: remember answers
Sometimes a program asks the very same small question again and again. Write each answer on a sticky note the first time, and later you only read the note.
- Some problems ask the same small question many times. Working it out each time wastes effort.
- A sticky note (a table of answers) stores each answer the first time. Later you read it instead of working it out.
- Writing answers down like this is called memoisation. It is one kind of dynamic programming.
- The code first checks the notes. If the answer is there, it returns at once.
- The savings grow fast: ways(30) needs over a million jobs without notes and only 30 with them.
Loading the playable lesson…