Algorithms
Dynamic Programming
Цели урока
- Understand the DP paradigm
- Master the 5-step approach
- Solve 1D and 2D problems
- Optimize memory usage
Предварительные знания
- Divide and Conquer
90% of algorithm interview problems are solved with DP. It's not magic - it's a technique: define the state, write the recurrence, find the answer.
- Autocorrect (edit distance)
- Git diff (LCS)
- Bioinformatics (sequence alignment)
- Finance (optimal strategies)
Richard Bellman and a name chosen to hide the math
Dynamic programming was created by Richard Bellman at the RAND Corporation in the early 1950s. The name has a famous backstory that Bellman told in his autobiography. RAND worked for the Air Force, and the Secretary of Defense Charles Wilson had an open hostility to mathematical research. Bellman needed a label for his work on multistage decision processes that would not sound like math. He picked dynamic, because it sounded active and could not be used in a pejorative way, and programming, which at the time meant planning and scheduling rather than writing code. The core idea is the principle of optimality: an optimal solution is built from optimal solutions to its subproblems, and overlapping subproblems are solved once and stored. That insight unified a huge class of optimization problems, from shortest paths to inventory control.
Idea: Subproblems + Memoization
**Dynamic Programming (DP)** = breaking into subproblems + caching results.