Question : Calculate the nth fibonacci number.
Lets explore the steps to coming up with DP solution :
1) Think of a recursive approach to solving the problem.
This part is simple.
We already know
fibo(n) = fibo(n - 1) + fibo(n - 2)
and we satisfy the condition of Yi < X
as
n - 1 < n
and n - 2 < n
.
2) Write a recursive code for the approach you just thought of.
int fibo(int n) {
if (n <= 1) return n;
return fibo(n - 1) + fibo(n - 2);
}
Try to think of the time complexity of the above function.
We analyzed it previously in one of the lessons on recursion. You can check it out again.
It is essentially exponential in terms of n.
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.
Ok. So, we try to save the value we calculate somewhere so that we dont compute it again. This is also called memoization.
Lets declare a global variable then.
int memo[100] = {0};
int fibo(int n) {
if (n <= 1) return n;
// If we have processed this function before, return the result from the last time.
if (memo[n] != 0) return memo[n];
// Otherwise calculate the result and remember it.
memo[n] = fibo(n - 1) + fibo(n - 2);
return memo[n];
}
4) Analyze the space and time requirements, and improve it if possible.
Lets look at the space complexity first. We have an array of size n allocated for storing the results which has space complexity of O(n).
We also have the stack memory overhead of recursion which is also O(n). So, overall space complexity is O(n).
Lets now look at the time complexity.
Lets look at fibo(n).
**Note: ** When fibo(n - 1) is called, it makes a call to fibo(n - 2). So when the call comes back to the original call from fibo(n), fibo(n-2) would already be calculated. Hence the call to fibo(n - 2) will be O(1).
Hence, T(n) = T(n - 1) + c where c is a constant.
= T(n - 2) + 2c
= T(n - 3) + 3c
= T(n - k) + kc
= T(0) + n * c = 1 + n * c = O(n).
And voila indeed! Thanks to DP, we reduced a exponential problem to a linear 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:54 | |
Regular Expression Match | 500 |
|
79:41 | |
Regular Expression II | 500 |
|
70:40 | |
Interleaving Strings | 500 |
|
62:14 |
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:44 | |
Paint House! | 200 |
|
41:35 | |
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:39 | |
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:54 | |
Ways to color a 3xN Board | 200 |
|
65:26 | |
Kth Manhattan Distance Neighbourhood | 200 |
|
64:27 | |
Best Time to Buy and Sell Stock atmost B times | 200 |
|
63:40 | |
Coins in a Line | 300 |
|
63:59 | |
Evaluate Expression To True | 350 |
|
72:21 | |
Egg Drop Problem! | 450 |
|
56:16 | |
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:34 | |
Max Sum Path in Binary Tree | 400 |
|
55:23 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Kingdom War | 200 |
|
60:45 | |
Maximum Path in Triangle | 200 |
|
33:37 | |
Maximum Size Square Sub-matrix | 200 |
|
44:49 | |
Increasing Path in Matrix | 200 |
|
47:20 | |
Minimum Difference Subsets! | 200 |
|
51:59 | |
Subset Sum Problem! | 200 |
|
45:46 | |
Unique Paths in a Grid | 300 |
|
34:08 | |
Dungeon Princess | 300 |
|
71:49 | |
Min Sum Path in Matrix | 300 |
|
30:33 | |
Min Sum Path in Triangle | 300 |
|
43:26 | |
Max Rectangle in Binary Matrix | 350 |
|
79:19 | |
Rod Cutting | 350 |
|
76:14 | |
Queen Attack | 350 |
|
61:42 | |
Dice Throw | 400 |
|
49:34 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Sub Matrices with sum Zero | 200 |
|
75:37 | |
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:39 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Chain of Pairs | 200 |
|
44:12 | |
Max Sum Without Adjacent Elements | 225 |
|
58:16 | |
Merge elements | 300 |
|
64:11 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Flip Array | 200 |
|
81:31 | |
Tushar's Birthday Party | 200 |
|
72:55 | |
0-1 Knapsack | 200 |
|
49:10 | |
Equal Average Partition | 350 |
|
75:08 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Potions | 200 |
|
52:35 |
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:51 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Unique Binary Search Trees II | 400 |
|
36:29 | |
Count Permutations of BST | 400 |
|
60:04 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Palindrome Partitioning II | 400 |
|
62:58 | |
Word Break | 400 |
|
68:16 |