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
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Dynamic Programming

Go to Problems

Level 1

Jump to Level 2

Level 2

Jump to Level 3

Level 3

Jump to Level 4

Level 4

Jump to Level 5

Level 5

Jump to Level 6

Level 6

Jump to Level 7

Level 7

Jump to Level 8

Level 8

Be a Code Ninja!

Dynamic Programming Concept

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.

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
79:58
Jump Game Array 225 41:16
Min Jumps Array 300 71:55
Tree dp
Problem Score Companies Time Status
Max edge queries! 200 56:29
Max Sum Path in Binary Tree 400 55:20
Suffix / prefix dp
Derived dp
Problem Score Companies Time Status
Chain of Pairs 200 43:50
Max Sum Without Adjacent Elements 225 58:15
Merge elements 300 62:43
Knapsack
Problem Score Companies Time Status
Flip Array 200
80:49
Tushar's Birthday Party 200 72:26
0-1 Knapsack 200 48:53
Equal Average Partition 350 73:47
Dp
Problem Score Companies Time Status
Potions 200 54:02
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:27
Multiply dp
Problem Score Companies Time Status
Unique Binary Search Trees II 400 36:20
Count Permutations of BST 400
60:52
Breaking words
Problem Score Companies Time Status
Palindrome Partitioning II 400 62:46
Word Break 400
IBM
67:55
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
+91 *
+91
Change Number
Graduation Year *
Graduation Year *
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 you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
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
Security Leadership
Database Administrator
Data Analyst
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test