Dynamic Programming

Go to Problems

Characteristics of Dynamic Programming

Before moving on to approaches to solve a DP problem, let us have a look at the characteristics of a problem upon which we can apply the DP technique.

We can apply DP technique to those problems that exhibit the below 2 characteristics:

1. Optimal Substructures

  • Any problem is said to be having optimal substructure property if its overall optimal solution can be evaluated from the optimal solutions of its subproblems.
  • Consider the example of Fibonacci Numbers.
    • We know that a nth Fibonacci number (Fib(n)) is nothing but sum of previous 2 fibonacci numbers, i.e: Fib(n) = Fib(n-1) + Fib(n-2). 

    • From the above equation, we can clearly deduce that a problem of size ‘n’ has been reduced to subproblems of size ‘n-1’ and ‘n-2’.

    • Hence, we can say that Fibonacci numbers have the optimal substructure property.

2. Overlapping Subproblems

  • Subproblems are basically the smaller versions of an original problem. Any problem is said to have overlapping subproblems if calculating its solution involves solving the same subproblem multiple times.
  • Let us take the example of finding nth Fibonacci number. Consider evaluating Fib(5). As shown in the breakdown of steps shown in the image below, we can see that Fib(5) is calculated by taking sum of Fib(4) and Fib(3) and Fib(4) is calculated by taking sum of Fib(3) and Fib(2) and so on. Clearly, we can see that the Fib(3), Fib(2), Fib(1) and Fib(0) has been repeatedly evaluated. These are nothing but the overlapping subproblems. 
Note: It is important for a problem to have BOTH the above specified characteristics in order to be eligible to be solved using DP technique.

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Dynamic Programming Problems

Greedy or dp
Problem Score Companies Time Status
Tushar's Birthday Bombs 200
80:13
Jump Game Array 225 41:16
Min Jumps Array 300 71:56
Tree dp
Problem Score Companies Time Status
Max edge queries! 200 56:38
Max Sum Path in Binary Tree 400 55:21
Suffix / prefix dp
Derived dp
Problem Score Companies Time Status
Chain of Pairs 200 44:02
Max Sum Without Adjacent Elements 225 58:15
Merge elements 300 63:20
Knapsack
Problem Score Companies Time Status
Flip Array 200
81:07
Tushar's Birthday Party 200 72:37
0-1 Knapsack 200 49:05
Equal Average Partition 350 74:12
Dp
Problem Score Companies Time Status
Potions 200 53:40
Adhoc
Problem Score Companies Time Status
Best Time to Buy and Sell Stocks II 225 40:18
Dp optimized backtrack
Problem Score Companies Time Status
Word Break II 350
IBM
68:39
Multiply dp
Problem Score Companies Time Status
Unique Binary Search Trees II 400 36:27
Count Permutations of BST 400
60:26
Breaking words
Problem Score Companies Time Status
Palindrome Partitioning II 400 62:54
Word Break 400
IBM
68:03
lock
Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket