 Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER ## Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a: College/University *
Enter the name of your college
Branch *
Year of completion * College/University *
Enter the name of your college
Branch *
Year of completion * Current Company *
Enter company name
Experience * ## You're all set! Full name *
Email *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy . ## Welcome back!

Email * Go to Problems

Be a Code Ninja!

# Dynamic Programming Examples

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.
`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 = {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.

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

2d string dp
Greedy or dp
Problem Score Companies Time Status
Tushar's Birthday Bombs 200
78:13
Jump Game Array 225 41:16
Min Jumps Array 300 71:44
Tree dp
Problem Score Companies Time Status
Max edge queries! 200 56:34
Max Sum Path in Binary Tree 400 55:07
Suffix / prefix dp
Problem Score Companies Time Status
Sub Matrices with sum Zero 200
73:24
Coin Sum Infinite 225 64:26
Max Product Subarray 300 65:03
Best Time to Buy and Sell Stocks I 300 28:13
Arrange II 350 71:34
Derived dp
Problem Score Companies Time Status
Chain of Pairs 200 42:09
Max Sum Without Adjacent Elements 225 58:10
Merge elements 300 57:43
Knapsack
Problem Score Companies Time Status
Flip Array 200
78:42
Tushar's Birthday Party 200 70:50
0-1 Knapsack 200 47:57
Equal Average Partition 350 71:49
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 67:29
Multiply dp
Problem Score Companies Time Status
Unique Binary Search Trees II 400 36:06
Count Permutations of BST 400
62:18
Breaking words
Problem Score Companies Time Status
Palindrome Partitioning II 400 62:02
Word Break 400 67:00

Problem Score Companies Time Status
Potions 200 56:52
Dice Throw 400 49:10
Double Increasing Series 200 45:07
Dice Rolls 300 27:32
Palindromic Substrings 200 25:36 Free Mock Assessment
Help us know you better for the best experience
Current Employer *
Enter company name