Best Time to Buy and Sell Stocks - K Transaction Allowed Dynamic Programming
Vložit
- čas přidán 7. 08. 2020
- Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. With this video, we explained the buy and sell stock questions of dynamic programming. In this video, we discuss the question where we are required to buy and sell stock with K transactions such that maximum profit is obtained from the transaction. In this problem,
1. You are given a number n, representing the number of days.
2. You are given n numbers, where ith number represents price of stock on ith day.
3. You are given a number k, representing the number of transactions allowed.
3. You are required to print the maximum profit you can make if you are allowed k transactions at-most.
Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).
To attempt and submit this question, click here: www.pepcoding.com/resources/o...
For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
#dynamicprogramming #buysellstocks
Have a look at our result: www.pepcoding.com/placements
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
Join us on Telegram: t.me/joinchat/UVTjJE83a-zFnPB
Though you were not in the top search list, still we all came here :-P , it's just the quality that drags us all down here. Awesome Sir!
Thankyou beta, I am glad.
With the love and respect which I get from you people one day we will surely reach heights and touch the sky.
Keep on supporting and loving Pepcoding😊
True
Sir nobody can explain like you. True legend.
Glad to know that you liked the content and thank you for appreciating.
The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
So, keep motivating, keep learning and keep loving Pepcoding😊
@@Pepcoding Thank You Sir
Hats off respect Sir, how much effort you put in each video especially like this one so we can understand easily, you are a god gift to us.
Best Explanation of this Problem. Thank You For such an amazing explanation.
Best Explaination of any dp concept till yet
Thanks for sharing for knowledge
This is Absolutely Gold content Available here for free, I wish someday I can become as good a teacher as you are .
sir I want to say thanks to you for all your efforts and the free and the best content that you have provided. I really appreciate your work. I also noticed that there are no ads not even at the beginning of any video. You are the best.
No matter what the difficulty of question is,
Sir by your explanation, question always seems easy
Thanks sir!!
😀
Thank you Sir, This one question is too much difficuilt and with your explanation i've got that in my head completely,
You are most welcome. Kaash log poori dekhein.
That is quality and depth sir . Thanks 🙏
Thank god you are here!!
Very good content. One of the best on youtube :)
He is all time favourite. No words just hats off to u Sumeet bhaiya
Awesome explanation and optimization!
Best explanation we can get for this sir!! Thankyou and keep posting!!!!
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
Dil jeet liya Sir. Bohot shukriya itne ache explanation k liye!
yahi to chaie
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
For clearing your doubts, you can join our community on telegram
t.me/pepcoding
Thank you, sir your all videos are amazing. itna ache se smajh aaya ki, Now I feel like ki stock market ka analysis kar lu.
Sir, aap itna humble attitude ke sathe itna accha kaise padate ho? Salute for all of your efforts.. I understand hole logic in one go of this video.. Aur pura concept v yaad rakhe ga.. Thank you so much sir..
Wooowh sir amazing explanation 🔥🔥🔥
sir you work so hard ,, your teaching methods are awesome.
Bohot acchi videos hai sir,
poori series eksath krke accha lag.
Ekk suggestion hai bas:
Agar aap thoda variable ke names aur code quality pr bhi help kre to kaafi accha hofa sir.
Companies aajkal kaafi judge krti hai code quality pr jab hum online coding interview me code kar rhe hote hai to.
sir after listening to the whole playlist, i have gained a lot of confidence in this topic, thank u
Glad it was helpful!pdtee rhiee beta ji
You are a true legend .
Excellent explanation!
Sir could you please make a video on the below question:
There is a rod of length N lying on x-axis with its left end at x = 0 and right end at x = N. Now, there are M weak points on this rod denoted by positive integer values(all less than N) A1, A2, …, AM. You have to cut rod at all these weak points. You can perform these cuts in any order. After a cut, rod gets divided into two smaller sub-rods. Cost of making a cut is the length of the sub-rod in which you are making a cut.
Your aim is to minimize this cost. Return an array denoting the sequence in which you will make cuts. If two different sequences of cuts give same cost, return the lexicographically smallest.
For eg,
N = 6
A = [1, 2, 5]
If we make cuts in order [1, 2, 5], let us see what total cost would be.
For first cut, the length of rod is 6.
For second cut, the length of sub-rod in which we are making cut is 5(since we already have made a cut at 1).
For third cut, the length of sub-rod in which we are making cut is 4(since we already have made a cut at 2).
So, total cost is 6 + 5 + 4.
Cut order | Sum of cost
(lexicographically | of each cut
sorted) |
___________________|_______________
[1, 2, 5] | 6 + 5 + 4 = 15
[1, 5, 2] | 6 + 5 + 4 = 15
[2, 1, 5] | 6 + 2 + 4 = 12
[2, 5, 1] | 6 + 4 + 2 = 12
[5, 1, 2] | 6 + 5 + 4 = 15
[5, 2, 1] | 6 + 5 + 2 = 13
So, we return [2, 1, 5].
I don't know why this channel is underrated, with fewer subs, and fewer views, though this channel deserves millions of views and subs❤❤💙💙.
Terrific explanation!!! Thank you very much!!
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@@Pepcoding yes sure
Legend!!!
best coding videos ever, keep creating such content
Thanks, will do!
Sir your explanation and thought process are the best
Pepcoding really makes questions easy
Thank you so much keep learning and keep loving Pepcoding😊
Awesome explanation 🤩
Thank you sir!!
Understood sir... Want more and more tutorials and practice questions
i'm so blessed to find you :-)
Thanks a lot!! share as much as you can
thank you so much!
Sir, kya hi samjhaya aapne ! Thanks.
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
i see these type of dp questions can be done in o(n) space by taking 2 1-d array one for current ith row and one for prev i-1th row.
crystal clear explanation : )
Padhte rahie
no one can teach better than this. 🔥🔥🔥🔥🔥 pepcoding rocks
Visit nados.pepcoding.com and sign up to NADOS, for better experience, precisely arranged content.
Don't forget to follow us on Instagram instagram.com/pepcoding/
Happy Coding!
Maza aa gya dear sir.
Your explanation is like super se bhi upar....
Keep going forward.
I have a dream to meet you one day....
45 minutes ..totally worth it!
Glad it was helpful!
Keep learning.
And for better experience, visit nados.io, where you will get well curated content and career opportunities.
You are amazing sir !!, your explanation is toooo gud
Thanks a ton
Your explanation is amazing sir
Glad you love the explanation, for better experience and well organised content sign up on nados.io and start learning.
Nice explanation sir, i like your Hindi language tone.
Nice explanation sir. I saw your brute force approach then I optimized it myself without watching video ahead.
Good.
Keep learning.
And for better experience, visit nados.io, where you will get well curated content and career opportunities.
My friend samrat told me about you, And you really are great sir..
Thanks and welcome
Really man, It was the best explanation one can ever give for this question This question is really tough. However, the way you explained made it too easy to understand even for a child and write a code of his own. Really Great!!! please make more and more videos on such hard problems
Glad it helped and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@@Pepcoding sure man!
Thanks ❤❤❤
GOD!!!!! THANKS SIR A LOT.
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like my efforts, I request a review
g.page/Pepcoding/review?rc
for a transaction to take place 2 days is required so for calculating the kth transaction at i th day it must be max of k th transaction i-1 th day and(k-1 transaction from 0 th day to i-2th day) is this statement valid
I search with the question name then scroll and search for ur image......heights of explanation. How can u explain things so easily?? After watching the explanation I code it by my self and It runs without even a single error. Thanks for all this.
Sir Great Explanation☺☺
I am glad you liked the video. I also hope that you are watching till end.
Will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms)
thanks
Thank you sir samajh mai aya aur maja bhi aya
I am glad you liked it. I also hope that you are watching till the end and trying to understand the what, how, and especially why of the problem.
If you like our efforts, we request a review - g.page/Pepcoding/review?rc
Thanks!
Welcome! For more content like this please visit nados.pepcoding.com
Sir, why you don't provide code in the description? If possible, please provide the code....it becomes very helpful for us. Understood today's concept. Thank you Sir
LEGEND.
🙏🙏🙏
Thank you Sir
Most welcome,
For better experience and well organised content sign up on nados.io
And for being updated follow us on Instagram instagram.com/pepcoding/
One we frame the recursive solution then implementing DP becomes intuitive like in this we didn't go this way like first thought recursively and then come to DP . In this question that part was not there
Thank you so much
Thankyou beta!
I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
awesome!!
Thank you! Cheers!
awesome explanation 🔥
Glad you liked it!
Keep learning.
And for better experience, visit nados.io, where you will get well curated content and career opportunities.
Bhai Inka ni ata ni ata ...par achanak se jaise hi ata h ...kya samajh ata h😂.. ❤ great sir luv u
we can also do in O(n) space
i prefer pepcoding videos not matter how big they are than watching web series
This is the only good explanation available on internet for this question 😌😌
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
@@Pepcoding yess
🏆gold
Can we do this question by calculating the sum of top K transaction profits from a list of profits if any number of transactions are allowed???
Awesome sir
Thank you so much Beta and If you like our efforts, please upvote the comments written by the students about Pepcoding here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
@Pepcoding bs ye clear koi bta sakta h when to use 1d array when to use 2d array!
??
This can be used to solve 2 transiction question
why don't we consider arr[i-1][j] while calculating maximum profit as it is possible to buy and sell on the same day. Lets consider we closed our 2 transactions on 5th day, and bought and sold again on the same day. Why wouldn't we consider that while finding max for 3 transactions on 5th day.
Awesome
Thank you.
sir one question, will the transaction not overlap
The Dp explanation part is fine, but discuss the recurrence relations before you dive into Dp.
🥳🥳
sir isme ek condition or dal skte h ki if [ k>= (no_of_days/2) ] then ye question infinite transcation allowed wala bn jyega
Yeah, right. Uske baad profit constant rahega.
@@Pepcoding Yes sir this condition is required to pass testcases of large values of k ..otherwise solution wouldn't have accepted.
1st time mai kuch samajh nhi aya...2nd time mai kuch kuch...3rd time mai kuch se thoda zada...in 4th time...kya mst content hai bhai....comment section kaha hai...hehehehhe...
Thank you
Pepcoding nd team...
Thank you!! very nice explanation. One question, can we solve the previous question (b&s stocks2 transactions allowed) with this dp method??
Yes , you definitely can.
sir aap logo ne comapny ke tags remove kar die hai update ke baad site pe, please unhe vapas la dijiye taki pata rahe questions konsi kopnsi comapnies me puche gaye hai
Sir, what about questions which can come as a combination of all the buy and sell stocks, like for eg, k transactions with cooldown/transaction fee?
aap soch ke dekhie ek baar. Mujhe k transaction with cooldown ek second mei samajh aa rha hai agar mujhe dono question aate hain to. Similarly transaction fee with cooldown bhi bhot araam se samajh aa rha hai jab 2no question aate ho to.
aur teeno ka combination bhi easy lag rha hai.
Suno, aap teeno ko individually karo. Firr meditate karo aur dry run karo, ki cooldown+fee hota to kya hota, fee+ktransaction hota to kya hota, cooldown+ktransaction hota to kya hota. Ab teeno sath soch kar dekhie.
Teeno questions ko individually kijie ek baar. teeno ka video available hai. Uske baad combination mujhe to simple lag rha hai. Khud baith ke tumhe analyse karna hoga.
sir can you share play list for all this problem.
Nice sir
Glad you liked it!
Keep learning.
And for better experience, visit nados.io, where you will get well curated content and career opportunities.
dil khush hogya aapse padh ke
Thank you
Is there a way to get this in English ?
your explanation is too good , but here we can do one more optimization here we can take "transactions(t)=min(n/2 ,t)".then it'll not give runtime error
Sir ye Foundation aur level 1 same hai? issmein 30 - Q of DP, level up matlab level 2? ussmein 60 - Q of dp tho ye IP kya hai?
btw thanks for helping students :)
Beta ip 4th year ke bacho ke liye hian jinpe time nahi bacha. Unko uttha ke leetcode kra dete hain
cant we solve it like -> find all transaction -> sort all transactions in descending -> if transactions
overlapping transaction kaise manage kroge isme , galat hoga ye solution
Can we solve the previous problem where we used 2 transactions, using this approach by putting k=2?
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
yes
sir iska inutition kaise aya ,ya ye kisi similar problem ka variation hai kya?
For clearing all your doubts visit on nados.pepcoding.com
Also don't forget to follow us on Instagram
Wow
Thank you.
sir why cant we buy and sell share at the same day where we are getting max profit
Bhai tb 0 profit hoga na
how to identify this problem is Dp problem
For better insight, visit nados.io, post your doubts, community will help you out there.
Please anyone help
Sir memoization me ye optimization kaise lagegi?
Beta, I regret to inform you that, I won't be able to answer/solve the personal doubts of each and every student over here. For clearing your doubts, you can join our community on telegram - t.me/pepcoding.
Shouldn't the answer be max of the last column?
For better experience and precisely arranged videos.
Visit - nados.pepcoding.com and sign up to NADOS.
Don't forget to follow us on Instagram instagram.com/pepcoding/
sir foundation ka dp pura ho gaya h?
foundation course pura dal chuka h ya abhi or baki h ?
han beta, foundation ka dp poora hai. usme 30 he questions hain. ab level2 mei 120 questions aur dalenge - 60 levelup ke, 60 IP ke. total dp questions 150 ho jaenge
Sir agr aap k transaction with fee/cooldown bhi krwadenge to bohot help hojaegi... Maine teeno videos dekhne k baad socha but sahi se logic bn ni paaya... thoda brief me btado ya kisi next video me short me btado
agr solution ho iska bhai abhi tk toh share kro ,mai cooldwon+fee kr paa rha hu ,but k transaction with cooldown/fee nhi kr pah rha
op cntnt
Sir telegram channel join nahi ho raha
Beta, dubara try kr k dekho, or you can contact us on 01140194461. Our team will help you with the same.
sir this solution is showing memory limit exceed on leetcode.
It is because when k gets greater than the prices.length/2, then it's equivalent to max profit from infinite transactions. Add that case, MLE will be gone.
What actually is the reason for this?
@@raghavbansal9020 I am getting TLE also after adding prices.length/2 condition, I am doing as => apply infinite transaction solution for k > prices.length condition, else do the same as of this video.