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:-  




Steps for how to solve a Dynamic Programming Problem

Similar Reads

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....

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....

Step 4: Adding memoization or tabulation for the state

...

Contact Us