 Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER ## Dynamic Programming

Last Updated: Nov 17, 2023
Go to Problems Dynamic Programming Complete all the problems in this Topic to unlock a badge
Completed
Go to Problems
Contents # 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.

## Dynamic Programming Problems

0/9
2D string DP 0/9
Simple array DP 0/3
Greedy OR DP 0/11
DP tricky 0/2
tree DP 0/14
Matrix DP 0/5
Suffix / prefix DP 0/3
Derived DP 0/4
Knapsack 0/1
DP 0/1 0/1
DP optimized backtrack 0/2
Multiply DP 0/2
Breaking words  Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket

## Video Courses By View All Courses Excel at your interview with Masterclasses Know More   Certificate included What will you Learn? Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+32 *
+32
Change Number
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer 