Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat blog/dinamik-dasturlash-haqida-batafsil

Dynamic Programming Basics and Classic Problems Explained

Dynamic programming is the family of algorithms that scares people most in contests and technical interviews — and becomes a favourite once you grasp its essence. We cover the core idea of DP, memoization versus tabulation and the classic problems.

Algorithms Views: 59
Dynamic Programming Basics and Classic Problems Explained — cover image
Dynamic programming is the family of algorithms that scares people most in contests and technical interviews — and becomes a favourite once you grasp its essence. We cover the core idea of DP, memoization versus tabulation and the classic problems.

Many programmers struggle when they reach Dynamic Programming (DP) while learning algorithms. Yet the essence of dynamic programming rests on one very simple piece of wisdom: "Store a result once you have computed it, and never compute it a second time!"

In this article we look at what dynamic programming is, how it works and its most famous classic problems, with practical examples.


1. When is dynamic programming used?

For a problem to be solvable with dynamic programming, it must have 2 key properties:

  1. Optimal Substructure: the globally optimal solution is built from the optimal solutions of its smaller parts.
  2. Overlapping Subproblems: the same subproblem is computed dozens or even millions of times during the recursion.

Classic example: the Fibonacci sequence

With the plain recursive approach $F(n) = F(n-1) + F(n-2)$:

  • to compute $F(5)$, $F(3)$ is computed twice and $F(2)$ three times;
  • time complexity: $O(2^n)$ — exponential! With $N = 50$ the program hangs for minutes;
  • with dynamic programming: we compute each $F(i)$ once and store it in an array;
  • the time complexity instantly drops to $O(n)$, a few microseconds!

2. Memoization (Top-Down) and Tabulation (Bottom-Up)

There are 2 classic ways to code DP solutions:

A) Top-Down (memoization):

We solve the problem recursively from the top down and write every result into a cache (for example, the memo array):

int memo[10005];

int fib(int n) {
    if (n <= 1) return n;
    if (memo[n] != -1) return memo[n]; // Take it from the cache
    return memo[n] = fib(n - 1) + fib(n - 2);
}

B) Bottom-Up (tabulation):

Starting from the smallest base case, we fill the array (the table) in a loop:

int dp[10005];

int fib(int n) {
    dp[0] = 0;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[n];
}

The Bottom-Up approach is usually preferred: it saves memory and avoids overflowing the recursion stack (Stack Overflow).


3. The most famous DP problems and their state transitions

  1. 0/1 Knapsack: Pack items of maximum total value into a knapsack of capacity $W$.
    • State: dp[i][w] — the maximum value using the first $i$ items with remaining capacity $w$.
    • Transition: dp[i][w] = max(dp[i-1][w], val[i] + dp[i-1][w - wt[i]]).
  2. Longest Common Subsequence (LCS): Find the longest common subsequence of two strings. The foundation of Git diff and bioinformatics (DNA analysis)!
  3. Longest Increasing Subsequence (LIS): The longest increasing subsequence of an array. With Binary Search it can be sped up from $O(N^2)$ to $O(N \log N)$.
  4. Coin Change: Make a given sum with the smallest number of coins.

How do you become a DP master?

The only key to mastering dynamic programming is more practice:

  • Start with 1D DP problems (for example, climbing stairs, House Robber).
  • Then move on to 2D DP (counting paths on a grid, the knapsack problem).
  • Try the Dynamic Programming problems collection on ALGOR!
Was it useful? Share it with friends:
Telegram

Related articles

All articles →