Best time to buy and sell stock 4 | Leetcode
Vložit
- čas přidán 7. 09. 2024
- This video explains the buy and sell stock 4 problem which is the extended version of buy and sell stock with maximum 2 transactions.I have explained the intuition for solving this problem by using simple examples.It is highly recommended to watch the previous related videos and then come to see this in order to completely understand the solution.All the useful related links are present below.I have first explained the recursion and backtracking technique with memoization dynamic programming and the second technique is by using state machine dynamic programming.I have taken sufficient examples in order to explain the concept in an easy way.I have also explained the code walkthrough at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
========================================================================
Join this channel to get access to perks:
/ @techdose4u
INSTAGRAM : / surya.pratap.k
SUPPORT OUR WORK: / techdose
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
TELEGRAM Channel LINK: t.me/codewithT...
TELEGRAM Group LINK: t.me/joinchat/...
=======================================================================
CODE LINK: gist.github.co...
USEFUL VIDEOS:-
Best time to buy and sell stock 2: • Best time to buy and s... Best time to buy and sell stock with cooldown: • Best time to buy and s... Best Time to Buy and Sell Stock III: • Best Time to Buy and S...
This one is so damn hard. Even though you explained it so well I am still blown away by how it works and why it works this way!!!! But still thank you so much, without you I won't even touch this one, yeah I won't even go there 😂
It's commendable that you are trying so hard 😊 You will get it soon.
I'm trying so hard to actually understand even after seeing the solution.
@@jeneyify It is very hard, don't beat yourself up for that.
great video! At 29:46 we were passing 3 before (k+1 now, and this is exactly what we have done before), and you said two.
The best solution I saw until now, better than solution from leetcode and its discussion.
It's easy for understanding and memorization, which is critical important for interview's sake.
Great job.
Thanks :)
thanks for the excellent explanation bro :-) Looked at various solutions but couldn't grasp the idea behind them. This is right on the money and well explained. Keep up the good work!!!
Thanks
you made this hard question so easy, Respect++
Thanks ++
Sir each and every time I see your video, I get impressed with the clear and crisp explanation. Thanks a lot :)
Fantastic explanation! Thank you so much.
Welcome :)
You are best. Great explanation
Thanks
The best explanation
Thanks 😊
this question was asked by oracle in first round
Nice to let everyone know :)
thankyou so much 😭😭😭😭😭😭😭 i really mean it
Welcome :)
Nice explanation ❤️
Thanks :)
This channel is awesome thank u
Welcome :)
great explanation
Thanks
Hi, the memoization solution is giving runtime error on testcase 209. May be because of high k value. So need to handle that too in code.
Sir please make more videos on dynamic programming
Killed it man❤️
Thanks
For each price, it will go through the dp[] state array. In a sense, I feel each price is being counted in 2K (Buy + Sell) transactions instead of one transaction. However, this algorithm still arrives at the correct solution. How does it prevent a price being over counted?
Thanks
n^2*k is bad solution for this ?
it will be great if I can read the subtitles.
Do we really need to allocate 2k+1 elements and go up to j=2k, if in reality we return 2k-1-th element?
Thanks for the great solution! Just wanted to ask a quick question about the state transition:
bought[j] = max(bought[j], sell[j-1] - prices[i]);
sell[j] = max(sell[j], bought[j] + prices[i]);
When we calculate bought[j] and buy a stock, it is sell[j-1] - prices[i].
However, when we sell a stock, how come it is bought[j] + prices[i] and NOT bought[j-1]??
correction its j-1
mastering iterative dp is tough
True
Bro i implemented this problem I got correct output but when I try to use memoization storing a element in two d array but I couldn't get a correct answer......my code is given below....will you help me bro.... whether we can apply memoization in this code
public static int maxProfit(int price[],int ind,int k,int profit){
if(k == 0) return profit;
if(ind >= price.length) return profit;
int max = 0;
for(int i=ind+1;i
Please ask this in telegram group for better and quicker help.
Did you find your your mistake ?
Here is shortest code:-
int maxProfit(int k, vector& prices) {
int n=prices.size();
k=min(k,n/2);
if(n
Can we make 3D memoization table into a 2D table?
Yea. You need to just maintain the states. You can use string as a key to maintain multiple states together and use a simple map.
@@techdose4u yes that would work, What I meant is, when we were using bottom-up we did it in 2D table, can we generate the same 2D table but Top-Down? In Top-Down why do we have to deal with one extra state? can we avoid it?
How to solve k transactions with cooldown
Sir could you cover leetcode substring concatenation
Graph Algo?
Graph algo is going on simultaneously. Don't worry.
Sir can u make a series on codeforces qiestions?
It will be very late. I am focussing on preparing for placements for now.
This approach is not intuitive at all
This approach sucks. It's easy if you use automata