What is dynamic programming, how does it work, and when to use it?

What is dynamic programming?

Dynamic programming is a powerful algorithmic technique used to solve complex problems efficiently.

Its essence lies in the idea of taking a large problem and breaking it down into a series of smaller, more manageable subproblems. Unlike other repair methods, the fundamental characteristic of dynamic programming is that these subproblems overlap; that is, the same subproblem is encountered and solved multiple times throughout the process.

To avoid the redundant work of recalculating the same solution over and over again, dynamic programming uses a clever approach: it stores the results of already solved subproblems (either by memoization or tabulation) in a data structure, such as a table or dictionary.

In this way, when the algorithm needs the solution to a subproblem it has seen before, it simply looks it up in its storage instead of calculating it from scratch.

Problem:

Create a function that, using the Fibonacci sequence formula, calculates and returns the corresponding number.

The problem asks us to create a function that, using the Fibonacci sequence formula, calculates and returns the result corresponding to a given number.

First, let's analyze what they're asking us to do:

¿What is the Fibonacci sequence?

The Fibonacci sequence is an infinite sequence of numbers where each term is the sum of the two preceding terms, usually starting with 0 and 1 (or 1 and 1).

That is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34…