Tutorial-4-(My Experiences)-Dynamic Programming
My video tutorials on competitive programming:-https://www.youtube.com/watch?v=Y4AK8Q7_wcM
Woohoo !!!
Part-4 has come!!
Party time?!!
Link to previous(3) tutorials :
- https://medium.com/@karangujar43/tutorial-3-my-experiences-dynamic-programming-c711b9ff1071
- https://medium.com/@karangujar43/tutorial-2-my-experiences-dynamic-programming-7254e35cec1a
- https://medium.com/@karangujar43/tutorial-my-experiences-1-dynamic-programming-5fa956967c6d
Description of this tutorial :
“It consists of the concept which strengthens your iterative dp skills(bottom-to-top) approach.”
Dp is the most feared topic in competitive-programming and coding interviews.
Why?
Because at most of the places on internet and standard books,it is taught in a way which is very formal, not very intuitive,only recursive approaches,etc.
In my tutorials, I have been successful to teach complex concepts because my way of teaching is totally different. I personally connect with the reader in my writings. I know how he/she feels and what they want. I clear the most basic things,let you grow a strong foundation in the topic,share my experiences which finally lets you fall in love with the hard topics :-) I love to make things easy for understanding!!
There is no step-by-step course/tutorial on internet to improve your dynamic programming skills and hence people get lost in the deep ocean .
Making these tutorials takes lots of my effort and time, so I am selling this tutorial-4 at a mere price of 10 rupees.(Indian Currency)[for the first 100 buyers]
It has reached the 100 buyers list.Now, the price of tutorial is:-20 Rupees.
This 4th tutorial will make your dp concepts much stronger, it is much more fantastic compared to the first three tutorials.
It will teach you how to think in a dp-way :-)
And soon enough, there will be no problem you cannot tackle!!
For Indians:-(Only Paytm/Phonepay)
- Email me “yes-dp-tutorial-4” and your contact number through which you will send the money.
- I will reply “yes” to that email with my contact number(paytm/phonepay)
- Transfer the money to that number and I will mail you the tutorial-4(in pdf format) in few minutes.
For people outside of India:-
- Just mail me “yes” and I will explain the further process in detail.
My email:-shalinitomar1344@gmail.com
This complete series will be of 25-parts(1,2,3,4,5…..25) , at the end of which you will be an expert in dp ;)
From tutorial-5 on-wards,it will contain video and practice problems as well for better understanding.
Now, lets discuss a nice dp problem which is not the part of 4th tutorial,just for fun(it won’t be as detailed as I explain in tutorials usually and its only for those who are a bit comfortable with dp) :-)
This question was asked to me by a 3rd-year undergraduate at some NIT. He was asked this problem during his interview for Microsoft Internship. He later messaged me for this problem’s solution. I think it is a good problem to brush up dp so I am answering it here :-)
Find the number of different ways in which groups (partitions) can be formed of a set and the maximum size of the group is 2?
For n=3,
if n=3, i.e, A,B,C are the three persons, different groups can be :
1)a/b/c
2) ab/c
3)a/bc
4)b/ac
So total 4. The output should be 4 for input n=3.
Finally, O(N) solution is:
dp(i)= dp(i-1)+(i-1)*dp(i-2) .
Explanation (1) :
Say {a1,a2……….a5}, we have,and we know its answer already,say its dp[5].
Now,if I try to add {a6} , what I can do is :->
1)Keep it alone.Keep it separate from everybody else in the group. So the answer is same as dp[5].
Hence. dp[6]=dp[5].
OR
2)Add it with one of the guys, say I join it with {a2}…so it looks like:→ {a2,a6}…{rest of the guys}, so dp[rest] is the answer, rest=6–2=4, so dp[6]=dp[4], but wait you can join ‘6’ with ‘1’ OR ‘2’ OR ‘3’ OR ‘4’ OR ‘5’. HENCE IN TOTAL ‘5’ CHOICES.
THEREFORE, dp[6]=5*dp[4].
Final conclusion:- dp[6]=5*dp[4]+dp[5].
That’s it.
Detailed Explanation(2) :
For n=2 :
{1/1}
{2}
are the solutions.
∴ dp[2]=2.
For n=3 :
{1/1/1}…….{a/b/c}
{2/1}………{ab/c}
{2/1}………{ac/b}
{1/2}……….{a/bc}
are the solutions.Therefore, dp[3]=4.
For n=4, you can simply add “1” to all of the above numbers of ‘dp[3]’.
Hence,
For n=4:
{1/1/1/1}….added ‘1’ at the end.
{2/1/1}….added ‘1’ at the end.
{2/1/1}….added ‘1’ at the end.
{1/2/1}….added ‘1’ at the end.
Hence, dp[4]=dp[3]=4.
But wait, there are few more numbers to add!
Also, you can add, ‘2’ to all numbers of dp[2]:-
{1/1/2}….added ‘2’ at the end
{2/2}….added ‘2’ at the end
But, these ‘2’ occur 3 times, in different formats in dp[4], hence,
dp[4]=dp[3] + x*dp[2] , where x=3,
if you try for n=5,
dp[5]=dp[4] + x*dp[3], where x=4.
So, we can see the pattern right ? :-)
Hence, for any ‘i’,
dp[i]=dp[i-1]+ (i-1)*dp[i-2] , run a for loop and you are done!
Link to tutorial-5:-
Please leave a like(clap) to let me know that you guys like it!
“If these tutorials have helped you even a bit, then please share it with your friends who might need it for their upcoming coding interviews/coding competition”
Write your review on my tutorials here:-
https://docs.google.com/forms/d/1QBfKiqueYUBDsz5wfBswSVNsfasthJjc6dUzzFUsxUc/edit