A *writes down "1+1+1+1+1+1+1+1 =" on a sheet of paper*
A : "What's that equal to?"
B : *counting* "Eight!"
A *writes down another "1+" on the left*
A : "What about that?"
B : *quickly* "Nine!"
A : "How'd you know it was nine so fast?"
A : "You just added one more"
A : "So you didn't need to recount because you remembered there were eight! Dynamic Programming is just a fancy way to say 'remembering stuff to save time later'"
This conversation has the essence of dynamic programming.
The idea is very simple, If you have solved a problem with the given input, then save the result for future reference, so as to avoid solving the same problem again.. shortly ‘Remember your Past’. If the given problem can be broken up in to smaller sub-problems and these smaller subproblems are in turn divided in to still-smaller ones, and in this process, if you observe some overlapping subproblems, then its a big hint for DP. Also, the optimal solutions to the subproblems contribute to the optimal solution of the given problem
Following are steps to coming up with a dynamic programming solution :
1. Think of a recursive approach to solving the problem.
Essentially expressing the problem P(X) in terms of P(Y) or an expression involving P(Yi)
where Yi is less than X.
The "less than" here could mean multiple things. if X is an integer, then it could mean less than arithmetically.
If X is a string, it could mean a substring of X.
If X is an array, it could mean a subarray of X, and so forth.
2. Write a recursive code for the approach you just thought of.
Lets say your function definition looks like this :
solve(A1, A2, A3 ... )
3. Save the results you get for every function run so that if solve(A1, A2, A3, ... ) is called again, you do not recompute the whole thing.
4. Analyze the space and time requirements, and improve it if possible.
And voila, we have a DP solution ready.
Lets explore this using an example where we see how DP improves the time complexity of solving the same problem.
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Longest Common Subsequence | 200 |
|
42:05 | |
Longest Palindromic Subsequence | 200 |
|
41:12 | |
Edit Distance | 300 |
|
47:01 | |
Repeating Sub-Sequence | 300 |
|
64:12 | |
Distinct Subsequences | 325 |
|
65:51 | |
Scramble String | 500 |
|
72:56 | |
Regular Expression Match | 500 |
|
79:44 | |
Regular Expression II | 500 |
|
70:41 | |
Interleaving Strings | 500 |
|
62:16 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Length of Longest Subsequence | 200 |
|
65:01 | |
Smallest sequence with given Primes | 200 |
|
67:33 | |
Largest area of rectangle with permutations | 200 |
|
76:53 | |
Tiling With Dominoes | 200 |
|
63:47 | |
Paint House! | 200 |
|
41:37 | |
Ways to Decode | 225 |
|
70:08 | |
Stairs | 225 |
|
15:14 | |
Longest Increasing Subsequence | 300 |
|
30:39 | |
Intersecting Chords in a Circle | 300 |
|
70:18 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Tushar's Birthday Bombs | 200 |
|
80:42 | |
Jump Game Array | 225 |
|
41:16 | |
Min Jumps Array | 300 |
|
71:56 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Longest Arithmetic Progression | 200 |
|
75:50 | |
N digit numbers with digit sum S | 200 |
|
73:15 | |
Shortest common superstring | 200 |
|
58:51 | |
Ways to color a 3xN Board | 200 |
|
65:27 | |
Kth Manhattan Distance Neighbourhood | 200 |
|
64:28 | |
Best Time to Buy and Sell Stock atmost B times | 200 |
|
63:42 | |
Coins in a Line | 300 |
|
63:59 | |
Evaluate Expression To True | 350 |
|
72:21 | |
Egg Drop Problem! | 450 |
|
56:23 | |
Best Time to Buy and Sell Stocks III | 700 |
|
64:45 | |
Longest valid Parentheses | 700 |
|
62:33 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Max edge queries! | 200 |
|
57:31 | |
Max Sum Path in Binary Tree | 400 |
|
55:23 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Kingdom War | 200 |
|
60:50 | |
Maximum Path in Triangle | 200 |
|
33:39 | |
Maximum Size Square Sub-matrix | 200 |
|
44:51 | |
Increasing Path in Matrix | 200 |
|
47:20 | |
Minimum Difference Subsets! | 200 |
|
52:01 | |
Subset Sum Problem! | 200 |
|
45:47 | |
Unique Paths in a Grid | 300 |
|
34:08 | |
Dungeon Princess | 300 |
|
71:54 | |
Min Sum Path in Matrix | 300 |
|
30:33 | |
Min Sum Path in Triangle | 300 |
|
43:27 | |
Max Rectangle in Binary Matrix | 350 |
|
79:20 | |
Rod Cutting | 350 |
|
76:18 | |
Queen Attack | 350 |
|
61:44 | |
Dice Throw | 400 |
|
49:41 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Sub Matrices with sum Zero | 200 |
|
75:39 | |
Coin Sum Infinite | 225 |
|
65:14 | |
Max Product Subarray | 300 |
|
65:25 | |
Best Time to Buy and Sell Stocks I | 300 |
|
28:13 | |
Arrange II | 350 |
|
73:42 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Chain of Pairs | 200 |
|
44:13 | |
Max Sum Without Adjacent Elements | 225 |
|
58:16 | |
Merge elements | 300 |
|
64:21 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Flip Array | 200 |
|
81:34 | |
Tushar's Birthday Party | 200 |
|
72:57 | |
0-1 Knapsack | 200 |
|
49:10 | |
Equal Average Partition | 350 |
|
75:11 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Potions | 200 |
|
52:53 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Best Time to Buy and Sell Stocks II | 225 |
|
40:18 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Word Break II | 350 |
|
68:54 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Unique Binary Search Trees II | 400 |
|
36:30 | |
Count Permutations of BST | 400 |
|
59:59 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Palindrome Partitioning II | 400 |
|
63:00 | |
Word Break | 400 |
|
68:18 |