Imagine you have a bag of coins where each coin is of value 5 dollars and you have to find the total amount of money available in that bag, What would you do to find the total amount in the bag?

You count the number of coins and multiply it with 5 dollars. So if the number of coins came out 100 then you have 500 dollars amount of money.

Now if I add 10 more coins of 5 dollars in that bag, How much is the amount now?

Image for post

Photo by Josh Appel on Unsplash

Yes, you are right, the answer is 550 dollars. Now take a moment and try to Imagine how you calculated this answer. Did you count all the number of coins in the bag and multiply it with 5 dollars?

No, you already knew that the bag had 100 coins and by adding 10 more coins the total number of coins is 110 and when you multiply 110 with 5 dollars you get 550 Dollars.

This approach to Problem Solving is called Dynamic programming. You store the answer of a subproblem in memory and use that to solve the problem.

But this approach cannot be used to solve all types of problems. So How do you Identify whether a problem can be solved with Dynamic Programming?

  • The problem can be divided into smaller subproblems
  • The Subproblems should be overlapping
  • It should show the optimal substructure property, for example: finding the shortest, longest path, or maximum or minimum amount.

The Dynamic Programming Problem can be solved with two approaches:

  1. Top-Down Approach
  2. Bottom-Up Approach

We will learn about both the approaches by solving a popular Dynamic Programming problem called the Partition Problem from LeetCode.

#problem-solving #javascript #interview-questions #dynamic-programming #leetcode

Dynamic Programming Made Easy
1.50 GEEK