Steps to solve a Dynamic programming problem:
- Identify if it is a Dynamic programming problem.
- Decide a state expression with the Least parameters.
- Formulate state and transition relationship.
- Apply tabulation or memorization.
Step 1: How to classify a problem as a Dynamic Programming Problem?
- Typically, all the problems that require maximizing or minimizing certain quantities or counting problems that say to count the arrangements under certain conditions or certain probability problems can be solved by using Dynamic Programming.
- All dynamic programming problems satisfy the overlapping subproblems property and most of the classic Dynamic programming problems also satisfy the optimal substructure property. Once we observe these properties in a given problem be sure that it can be solved using Dynamic Programming.
Step 2: Deciding the state
Dynamic Programming problems are all about the state and its transition. This is the most basic step which must be done very carefully because the state transition depends on the choice of state definition you make.
State:
A state can be defined as the set of parameters that can uniquely identify a certain position or standing in the given problem. This set of parameters should be as small as possible to reduce state space.
Example:
In our famous Knapsack problem, we define our state by two parameters index and weight i.e DP[index][weight]. Here DP[index][weight] tells us the maximum profit it can make by taking items from range 0 to index having the capacity of sack to be weight. Therefore, here the parameters index and weight together can uniquely identify a subproblem for the knapsack problem.
The first step to solving a Dynamic Programming problem will be deciding on a state for the problem after identifying that the problem is a Dynamic Programming problem. As we know Dynamic Programming is all about using calculated results to formulate the final result.
So, our next step will be to find a relation between previous states to reach the current state.
Step 3: Formulating a relation among the states
This part is the hardest part of solving a Dynamic Programming problem and requires a lot of intuition, observation, and practice.
Example:
Given 3 numbers {1, 3, 5}, The task is to tell the total number of ways we can form a number N using the sum of the given three numbers. (allowing repetitions and different arrangements).
The total number of ways to form 6 is: 8
1+1+1+1+1+1
1+1+1+3
1+1+3+1
1+3+1+1
3+1+1+1
3+3
1+5
5+1
The steps to solve the given problem will be:
- We decide a state for the given problem.
- We will take a parameter N to decide the state as it uniquely identifies any subproblem.
- DP state will look like state(N), state(N) means the total number of arrangements to form N by using {1, 3, 5} as elements. Derive a transition relation between any two states.
- Now, we need to compute state(N).
How to Compute the state?
As we can only use 1, 3, or 5 to form a given number N. Let us assume that we know the result for N = 1,2,3,4,5,6
Let us say we know the result for:
state (n = 1), state (n = 2), state (n = 3) ……… state (n = 6)
Now, we wish to know the result of the state (n = 7). See, we can only add 1, 3, and 5. Now we can get a sum total of 7 in the following 3 ways:
1) Adding 1 to all possible combinations of state (n = 6)
Eg : [ (1+1+1+1+1+1) + 1]
[ (1+1+1+3) + 1]
[ (1+1+3+1) + 1]
[ (1+3+1+1) + 1]
[ (3+1+1+1) + 1]
[ (3+3) + 1]
[ (1+5) + 1]
[ (5+1) + 1]2) Adding 3 to all possible combinations of state (n = 4);
[(1+1+1+1) + 3]
[(1+3) + 3]
[(3+1) + 3]3) Adding 5 to all possible combinations of state(n = 2)
[ (1+1) + 5](Note how it sufficient to add only on the right-side – all the add-from-left-side cases are covered, either in the same state, or another, e.g. [ 1+(1+1+1+3)] is not needed in state (n=6) because it’s covered by state (n = 4) [(1+1+1+1) + 3])
Now, think carefully and satisfy yourself that the above three cases are covering all possible ways to form a sum total of 7;
Therefore, we can say that result for
state(7) = state (6) + state (4) + state (2)
OR
state(7) = state (7-1) + state (7-3) + state (7-5)
In general,
state(n) = state(n-1) + state(n-3) + state(n-5)
Below is the implementation of the above approach:
C++
// Returns the number of arrangements to // form 'n' int solve( int n) { // base case if (n < 0) return 0; if (n == 0) return 1; return solve(n-1) + solve(n-3) + solve(n-5); } |
Java
// Returns the number of arrangements to // form 'n' static int solve( int n) { // base case if (n < 0 ) return 0 ; if (n == 0 ) return 1 ; return solve(n- 1 ) + solve(n- 3 ) + solve(n- 5 ); } // This code is contributed by Dharanendra L V. |
Python3
# Returns the number of arrangements to # form 'n' def solve(n): # Base case if n < 0 : return 0 if n = = 0 : return 1 return (solve(n - 1 ) + solve(n - 3 ) + solve(n - 5 )) # This code is contributed by GauriShankarBadola |
C#
// Returns the number of arrangements to // form 'n' static int solve( int n) { // base case if (n < 0) return 0; if (n == 0) return 1; return solve(n-1) + solve(n-3) + solve(n-5); } // This code is contributed by Dharanendra L V. |
Javascript
<script> // Returns the number of arrangements to // form 'n' function solve(n) { // base case if (n < 0) return 0; if (n == 0) return 1; return solve(n-1) + solve(n-3) + solve(n-5); } // This Code is Contributed by Harshit Srivastava </script> |
Time Complexity: O(3N), As at every stage we need to take three decisions and the height of the tree will be of the order of n.
Auxiliary Space: O(N), The extra space is used due to the recursion call stack.
The above code seems exponential as it is calculating the same state again and again. So, we just need to add memoization.
Step 4: Adding memoization or tabulation for the state
This is the easiest part of a dynamic programming solution. We just need to store the state answer so that the next time that state is required, we can directly use it from our memory.
Adding memoization to the above code
C++
// initialize to -1 int dp[MAXN]; // this function returns the number of // arrangements to form 'n' int solve( int n) { // base case if (n < 0) return 0; if (n == 0) return 1; // checking if already calculated if (dp[n]!=-1) return dp[n]; // storing the result and returning return dp[n] = solve(n-1) + solve(n-3) + solve(n-5); } |
Java
// initialize to -1 public static int [] dp = new int [MAXN]; // this function returns the number of // arrangements to form 'n' static int solve( int n) { // base case if (n < 0 ) return 0 ; if (n == 0 ) return 1 ; // checking if already calculated if (dp[n]!=- 1 ) return dp[n]; // storing the result and returning return dp[n] = solve(n- 1 ) + solve(n- 3 ) + solve(n- 5 ); } // This code is contributed by Dharanendra L V. |
Python3
# This function returns the number of # arrangements to form 'n' # lookup dictionary/hashmap is initialized def solve(n, lookup = {}): # Base cases # negative number can't be # produced, return 0 if n < 0 : return 0 # 0 can be produced by not # taking any number whereas # 1 can be produced by just taking 1 if n = = 0 : return 1 # Checking if number of way for # producing n is already calculated # or not if calculated, return that, # otherwise calculate and then return if n not in lookup: lookup[n] = (solve(n - 1 ) + solve(n - 3 ) + solve(n - 5 )) return lookup[n] # This code is contributed by GauriShankarBadola |
C#
// initialize to -1 public static int [] dp = new int [MAXN]; // this function returns the number of // arrangements to form 'n' static int solve( int n) { // base case if (n < 0) return 0; if (n == 0) return 1; // checking if already calculated if (dp[n]!=-1) return dp[n]; // storing the result and returning return dp[n] = solve(n-1) + solve(n-3) + solve(n-5); } // This code is contributed by Dharanendra L V. |
Javascript
<script> // initialize to -1 let dp = new Array(MAXN); // this function returns the number of // arrangements to form 'n' function solve(n) { // base case if (n < 0) return 0; if (n == 0) return 1; // checking if already calculated if (dp[n]!=-1) return dp[n]; // storing the result and returning return dp[n] = solve(n-1) + solve(n-3) + solve(n-5); } // This code is contributed by avanitrachhadiya2155 </script> |
Time Complexity: O(n), As we just need to make 3n function calls and there will be no repetitive calculations as we are returning previously calculated results.
Auxiliary Space: O(n), The extra space is used due to the recursion call stack.
Another way is to add tabulation and make the solution iterative. Please refer to tabulation and memoization for more details.
Dynamic Programming comes with lots of practice. One must try solving various classic DP problems that can be found here.
You may check the below problems first and try solving them using the above-described steps:-
S. No. |
Problem |
Practice link |
1 |
||
2 |
||
3 |
||
4 |
||
5 |
This article is contributed by Nitish Kumar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!