Dynamic programming problems has several variations. Because of that, programmers sometimes struggle to decide whether a problem can be solved with dynamic programming and that process can take time.
Understanding this idea is important because once we decide that a problem can be solved using dynamic programming, we need to follow some specific steps to solve it. In practice, two popular categories of problems can be solved using dynamic programming: (1) Optimisation problems, and (2) Counting problems.
1. Optimisation problem
In optimisation problems, we need to find an optimal solution like minimum, maximum, longest, shortest, etc. A brute-force solution idea for such problems explore every possible solution using recursion and identify optimal solutions among them. Unfortunately, the time complexity of such solutions can be exponential because the same subproblem recurs repeatedly.
Optimisation problem example 1: The longest common subsequence problem
Given two strings X and Y of size m and n**,** find the length of the longest common sequence. A common subsequence of two strings is a subsequence that is common to both strings. If there is no common subsequence, return 0.
There can be multiple common subsequences of the longest length but need to just return the length of the longest common subsequence. So this is an optimisation problem where the solution space is very large. If string X has size m, then there will be 2^m — 1 possible subsequence. How? The idea is simple!
There would be two choices for each character in a string: Either it is part of the subsequence or not part of the subsequence. To find the total number of subsequences, we need to multiply the possibility for all m characters, which is 2^m (multiply 2 m times). If we ignore the possibility of empty subsequences, the total number of subsequences = 2^m — 1. Similarly, string Y of size n has 2^n — 1 subsequence.
Let's understand this with an example.
Suppose X[] = "ABC" which has three characters.
If we take 1 when character is part of the
subsequence and take 0 not part of the subsequence.
Then we can generate all 8 subsequences by masking
the input string with the bits of all 3 size bianry numbers.
A B C
0 0 0 => Empty
0 0 1 => C
0 1 0 => B
0 1 1 => BC
1 0 0 => A
1 0 1 => AC
1 1 0 => AB
1 1 1 => ABC
If we have m size string then total count
of m size bianry numbers will be 2^m.One basic idea would be to explore all possible subsequences of both strings, compare each subsequence of X with each subsequence of Y and find the common and longest among them. So this solution would be exponential time and highly inefficient.
We can also observe the pattern of overlapping subproblems when we design a recursive solution. Repeatedly solving these subproblems is one reason for exponential time complexity. We can use a top-down or bottom-up dynamic programming approach to reduce the time complexity to O(mn), using O(mn) space. We can even go a step further and solve the problem using O(n) space.
Optimisation problem example 2: The max subarray sum problem
Given an array A of size n containing positive and negative integers, the goal is to find indices i and j, where 1 ≤ i ≤ j ≤ n, such that A[i] + A[i + 1] + … + A[j] is maximum. In other words, find a contiguous subarray A[i], A[i + 1], …, A[j] whose sum is as large as possible.
This is an optimisation problem because we want to find the subarray with the maximum sum. The solution space consists of all pairs of indices i and j such that 1 ≤ i ≤ j ≤ n. So, there are O(n²) possible subarrays. Note that all these subarray sums are solution candidates for the problem (the optimum is the one which provide the maximum sum).
We can easily solve this problem by calculating all the possible n² subarray sums and taking the maximum among them. This will take O(n²) time. But using DP, we can develop a simple and elegant O(n) time algorithm that is also easy to implement.
Other examples of optimisation problems
- Longest Increasing subsequence
- Longest Palindromic Substring
- Max Product Subarray
- Maximal square
- Minimum coin change problem
- Minimum number of jumps to reach the end
2. Counting Problem
In this type of problem, we need to count all occurrences of a pattern or constraint. A recursive solution explores all possible ways, which can lead to many recursive calls because of repeated subproblems.
Counting problems Example 1: Count the number of ways to reach the nth stair
There is a staircase of n steps and you can climb either 1 or 2 steps at a time. We need to count and return the total number of unique ways to climb the top of the staircase. The order of steps matters.
This is a counting problem and there are several ways to reach the nth stair. If we think recursively, there are two ways to reach the nth stair: Either from (n-1)th stair by taking 1 step or from (n-2)th stair by taking 2 steps. So total number of ways to reach nth stair = Total number of ways to reach (n-1)th stair + Total number of ways to reach (n-2)th stair.
So recurrence relation is: climb(n) = climb(n-1) + climb(n-2), where climb(1) = 1 and climb(2) = 2. This looks similar to recurrence relation of the Fibonacci series so we can apply the idea of dynamic programming.

Counting problems Example 1: Count paths from the top left to the bottom right of an MxN matrix
You have given an NxM grid and you are at the starting point i.e. 1st row and 1st column or (0,0). You can move either downward(D) or rightward(R). So problem is to find the total number of paths starting from the first cell to the last cell i.e. (N, M).
Solution via Combinatorics
We can observe that to get from (0, 0) to (N, M), we must move N steps downward and M steps to the right. So, how do we count the total number of paths? If we look carefully at each cell, we have two choices: move downward or move right. But we must make exactly N downward moves and M rightward moves. For example, we can reach the last cell by following a path such as RRDRDRDDRD…, with exactly N Ds and M Rs (R represents a move to the right, and D represents a move downward).
The problem can be solved using simple combinatorics. Every path consists of N downward moves and M rightward moves. Since each path has N + M moves, the paths differ based on which N moves are downward. So, the total number of paths is the number of ways to choose N downward moves from N + M moves: C(M+N, N) = (M + N)!/(N!)(M!).
We can also think about it another way. We can reach the right end using M rightward moves out of the N + M total moves. Therefore, the total number of paths is the number of ways to choose M moves from N + M moves: C(M+N, M) = (M + N)!/(N!)(M!). In both cases, the answer is the same i.e. (M + N)!/(N!)(M!).
Recursive solution: We can reach the cell (M, N) from (N-1, M) and (N, M-1) cells. So the total number of ways to reach (M, N)th cell = total number of ways to reach (N-1, M) th cell + the total number of ways to reach (N-1, M)th cell => pathCount(N,M) = pathCount(N-1,M) + pathCount(N,M-1).
As we can see, the total number of paths can be large. A recursive solution would repeat many subproblems, so we can use dynamic programming to improve its efficiency.
Other examples of counting problems
- Count ways to make the change
- Count of possible Binary Search Trees with n keys
- Count possible decodings of a given digit sequence
- Count the number of binary strings without consecutive 1’s
- Count all palindromic subsequences in a string
- Count of strings that can be formed using a, b and c under given constraints
- Count of different ways to express n as the sum of 1, 3 and 4
A critical note to identify a dynamic programming problem
If we have an optimisation or counting problem, one simple approach is to design a recursive solution, draw its recursion tree for a sample input and identify any repeated subproblems. If subproblems are repeated, the problem can be solved using dynamic programming.
But this approach may take some time because it involves two critical steps: designing the recursive solution and drawing the recursion tree. Is there a way to identify such problems more quickly? Here is an idea using mathematics or combinatorics that can work efficiently in many situations.
In case of optimisation or counting, we can use combinatorics to count all possible solution candidates. If the number of possible solution candidates is very large or exponential, we can apply dynamic programming to solve the problem efficiently. The reason is simple: a recursive solution will explore all possible candidates, which will lead to exponential time and repeated subproblems.
Enjoy learning, Enjoy algorithms!
