Best time to buy and sell stock with cooldown | Leetcode

  Рет қаралды 34,432

Techdose

Techdose

Күн бұрын

Пікірлер: 122
@PRANAVMAPPOLI
@PRANAVMAPPOLI 2 жыл бұрын
I have read leetcode best answer 4-5 times to understand the diagram methoad and failed. But this single video make it all cristal clear😻🖤
@theghostwhowalk
@theghostwhowalk 4 жыл бұрын
Liked the State diagram way!
@techdose4u
@techdose4u 4 жыл бұрын
I personally liked it too. So, I skipped tabulation DP approach.
@abhishekks6782
@abhishekks6782 4 жыл бұрын
I have one word for you. You are "GENIUS", thank you so much sir for creating this kind of wonderful contents and helping many students and aspirants like me. I am out of words 🙏
@techdose4u
@techdose4u 4 жыл бұрын
Welcome bro :)
@vvk6260
@vvk6260 4 жыл бұрын
@@techdose4u who came up with this, that is amazing..
@siddharthsaxena6495
@siddharthsaxena6495 3 жыл бұрын
great explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@yuvrajsinghrathore4919
@yuvrajsinghrathore4919 3 жыл бұрын
I was thinking of memoization technique but your state diagram method is much optimized. Loved it
@drishtdyumnshrivastava5313
@drishtdyumnshrivastava5313 2 жыл бұрын
this really made the concept crystal clear thank you...... as i always say ur channel is very underrated..
@jaden2582
@jaden2582 4 жыл бұрын
Best illustration on three-state approach on youtube for this problem.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@K_EC_AushoRoup
@K_EC_AushoRoup 4 жыл бұрын
You know what sir, I have solved this question by Recursion + Memoization (i.e., DP) but I came to your channel just for fun, and I Saw there is a Thumbnail saying Machine State Diagram approach for this question. And, Now I got learned 2 new Approaches => Valley peak + State machine algorithms. Previously I have used state diagram only in my ECE course. Thanks. I learned something new.
@techdose4u
@techdose4u 4 жыл бұрын
Nice 😊
@trishulcurtis1810
@trishulcurtis1810 4 жыл бұрын
Whats the time complexity of recursive memoization ?
@anandbangad2966
@anandbangad2966 4 жыл бұрын
In the state diagram how are we considering the cool off days, what if the cool of day is more than 1 ?
@sharmamukul938
@sharmamukul938 3 жыл бұрын
I believe in that case , we will have only change the way we compute the values of states which are directly linked to the selling state , i.e, we will have to modify the way we calculate inHand state as now : inHand[i] = MAX{inHand[i-1] , sell[i-k]}.
@VarunMittal-viralmutant
@VarunMittal-viralmutant 3 жыл бұрын
Thanks for the great video. I have 1 doubt in the state machine approach though, is going from "sell" ---> "no stock" state implicitly considering the "cooldown" ? Since we don't talk about the cooldown at all while using the state machines, I got confused where that is coming into picture. If there was no cooldown, then also the state machine approach can be used ?
@karankhara1312
@karankhara1312 2 жыл бұрын
Thanks for the great explanation!
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@BaishaliGhosh13
@BaishaliGhosh13 4 жыл бұрын
Super explanation with great clarity. Learnt a new technique. Thanks!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@rishabhgoyal8110
@rishabhgoyal8110 4 жыл бұрын
God bless you Man!
@techdose4u
@techdose4u 4 жыл бұрын
❤️
@junxuchen2851
@junxuchen2851 4 жыл бұрын
The state machine algorithm is incredible... Can't imagine how to come up an algorithm like this
@techdose4u
@techdose4u 4 жыл бұрын
It's very difficult 😅
@surabhisingh9956
@surabhisingh9956 3 жыл бұрын
Wow, such a good explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@code_life4835
@code_life4835 4 жыл бұрын
Great Explanation! Thanks a lot, Sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@suyashlokhande1981
@suyashlokhande1981 3 жыл бұрын
simply awsome
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@piyushgoyal9633
@piyushgoyal9633 3 жыл бұрын
You greatly analyzed the vietnam dude discussing this finite state diagram.I find it very difficult to understand.I think it need a lot of work to understand that.Thank you for making us better understand by taking in depths of what other person told
@tushardhote6393
@tushardhote6393 4 жыл бұрын
Sir, Super explanation with great clarity. Learnt a new technique. But in State transition I'm getting how you are tackling cooldown corner case and how can we apply this for d-days? If possible then please explain..would be of great help.
@techdose4u
@techdose4u 4 жыл бұрын
Try to seebthe memoization + recursion code for cooldown. I have explained where you need to make change to solve for D-days.
@tushardhote6393
@tushardhote6393 4 жыл бұрын
@@techdose4u Okay fine for D-days Sorry it was mistyped. My query was "In State transition I'm not getting how are you tackling cooldown corner case?"
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
that dp state diagram was so well explained sir. it will be so good if you cover these types of videos on dp. thanks again sir for your efforts
@techdose4u
@techdose4u 4 жыл бұрын
Okay sure
@andriidanylov9453
@andriidanylov9453 Жыл бұрын
Great job. Appreciate
@sumonchatterjee5337
@sumonchatterjee5337 4 жыл бұрын
awesome explanation both with dp and state diagram
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@harshitsharma7871
@harshitsharma7871 3 жыл бұрын
Very well explained. Appreciate it.
@neerajchoudhary3709
@neerajchoudhary3709 3 жыл бұрын
bhai bhai, kya baat hai, thanks man
@trineshreddybs842
@trineshreddybs842 4 жыл бұрын
keep sharing your knowledge of problem solving SIR.It will be very helpful for us
@prateeksinghal630
@prateeksinghal630 4 жыл бұрын
I loved the memoized code !!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@anushkagupta5917
@anushkagupta5917 4 жыл бұрын
how is the state approach taking cooldown into account?
@shushantgaur9420
@shushantgaur9420 4 жыл бұрын
Awesome approach, especially second one. But @TECH DOSE I'm still not getting how we are solving the cool-down period of 1 day here. Maybe because it's specially for one day period... Maybe, can you explain about 3 days of cooldown with state machine?
@techdose4u
@techdose4u 4 жыл бұрын
You should watch the next version of this problem and my solution
@shushantgaur9420
@shushantgaur9420 4 жыл бұрын
@@techdose4u all right👍. Although I was going to watch it anyways, cause it's really good content :) Thanks again
@techdose4u
@techdose4u 4 жыл бұрын
@@shushantgaur9420 nice :)
@shaziasamreen8584
@shaziasamreen8584 4 жыл бұрын
Great explaination 👏
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@mystryb345
@mystryb345 4 жыл бұрын
Wow what a explanation sir finally i understood all type problem of it..Sir if possible make video on at max K transactiom
@techdose4u
@techdose4u 4 жыл бұрын
Sure :)
@sameekshabhatia3200
@sameekshabhatia3200 3 жыл бұрын
This was so clear.Very well explained .Thank you so much.
@rishabh2055
@rishabh2055 3 жыл бұрын
Good insightful Explanation 👍
@ankitgoyal34
@ankitgoyal34 4 жыл бұрын
Thank you for explanation!!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@slimysnail6071
@slimysnail6071 Жыл бұрын
What is the time complexity for the recursion approach?
@VivekMishra-hd7mg
@VivekMishra-hd7mg 4 жыл бұрын
very well explained
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@amchourasia
@amchourasia 3 жыл бұрын
Perfect
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@firstjm9071
@firstjm9071 17 күн бұрын
Thank you!
@techdose4u
@techdose4u 17 күн бұрын
welcome :)
@666Imaginative
@666Imaginative 2 жыл бұрын
thank you so much sir
@mishikaraj5153
@mishikaraj5153 4 жыл бұрын
well explained Sir!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@amitavamozumder73
@amitavamozumder73 3 жыл бұрын
please make a video on how to identify state machine problems.
@techamet428
@techamet428 4 жыл бұрын
like usual.. beautifully explained 🔥🔥
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
Great video 😇 Thank you so much to explain always in very deeply
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@prasad.patil12
@prasad.patil12 4 жыл бұрын
Great explanation 👍
@vishalverma920
@vishalverma920 2 жыл бұрын
what is the time complexity of recursive solution after memoization ??
@pramodsinghyadav7580
@pramodsinghyadav7580 3 жыл бұрын
great explanation.I was looking for memoziation method which code is explained here nicely
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@deepakpoojari9019
@deepakpoojari9019 4 жыл бұрын
Thanks man
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@animeshjain4594
@animeshjain4594 3 жыл бұрын
Great explanation .... But I am still not getting in state diagram method where we took the cooldown in consideration .... I tried with many examples though it is giving the right answer but how?
@shubhamgiri2840
@shubhamgiri2840 4 жыл бұрын
Hey ! I really liked your videos and and how explain the concepts. Can u put another variant of this problem where there can be multiple transactions on a single day (like buying on 2 consecutive days and selling both of them together on one day or 2 different days). It would be very kind of u if u could help me in this
@Arunkumar-pg8kx
@Arunkumar-pg8kx 4 жыл бұрын
What if cooldown period is 2 days (n days)
@financewithsom485
@financewithsom485 4 жыл бұрын
the 2nd method is only for cooldown of 1 day right??
@techdose4u
@techdose4u 4 жыл бұрын
Yes. If you can modify the state machine then it may also work for K days :)
@lobsanggyatso6280
@lobsanggyatso6280 4 жыл бұрын
Sir can u give me some tip on dp , I can solve problem in recursion and but when it come to dp then I always stuck specially bottom-up
@techdose4u
@techdose4u 4 жыл бұрын
Keep practicing. It's difficult to master DP. You need to be patient and keep practicing. If was easy then everyone would master it. Keep patience and practice. You will improve.
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
@@techdose4u thanking you 😇
@prateeksinghal630
@prateeksinghal630 4 жыл бұрын
Don't focus on bottom-up dp too much!! If you can write the correct recursive code for the problem then you are on the right track !! Just try to learn how to memoize the simple recursive code as it is just adding 3-4 lines to your code and it gives the same time complexity as the bottom-up code using tabulation. Once you are able to write the memoized code then it's just a few steps more to convert that into the iterative approach using bottom-up dp !!
@mvee
@mvee 4 жыл бұрын
Great Video Man! Here's the state machine algorithm in O(1) space (no arrays): class Solution { public int maxProfit(int[] prices) { int sell = 0; int noStock = 0; int inHand = Integer.MIN_VALUE; for(int price : prices){ int temp = sell; // 0 // For in hand, we can either maintain or we buy inHand = Math.max(inHand, noStock - price); sell = Math.max(sell, inHand + price); noStock = Math.max(temp, noStock); } return Math.max(noStock, sell); } }
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@shushantgaur9420
@shushantgaur9420 4 жыл бұрын
@Michael Vandi How about we first find (Inside for loop) Temp= inhand; Inhand=max(nostock- price[i], inhand); Nostock= max(nostock, sell); Sell= temp+ price[I]; Reason: at 18:45 @TECH DOSE explained that you cannot remain in the same state for more than one day.... But according to the upper code you are assuming the max( previous day, and the current value). For example price[5]= {1, 2, 3, 0, 2}; (same example as tech dose) The values of fourth day of selling would be -1 because we are buying on first day and selling on 4 day... But according to the above code 4th day value is calculated by max( sell, inhand+ price[i]) that would give us 2 meaning we didn't sell on the fourth day but the 3rd. Or you can say that means you are considering the case where you sell on the third day and on the fourth day as well. Although it might not change the end result but I felt like telling you.
@mvee
@mvee 4 жыл бұрын
@@shushantgaur9420 ohh, I see what you mean. Thank you for the clarification!
@shushantgaur9420
@shushantgaur9420 4 жыл бұрын
@@mvee no problem bro:)
@gauravagarwal8592
@gauravagarwal8592 3 жыл бұрын
Why you say K+1 instead of K for the K cool Down period ? Can someone explain please?
@shahperzia2111
@shahperzia2111 3 жыл бұрын
because if say k=0; this will mean you are buying and selling same day but it is not possible , so in general you have to use +1 and here you also have k cooldown days , so k+1
@gauravagarwal8592
@gauravagarwal8592 3 жыл бұрын
@@shahperzia2111 K is the cool down period it means once u sold the stock u cant buy on next day . u will have to skip one day . so in the case of k =1 we have to skip 2 days will that be okay. kindly correct me if understanding is wrong.
@shahperzia2111
@shahperzia2111 3 жыл бұрын
@@gauravagarwal8592 yes for k=1 with can buy i+2 th day
@subhamtripathi1997
@subhamtripathi1997 3 жыл бұрын
sorry , but didnt we skip the "2" in the second valley?
@amanrai8010
@amanrai8010 4 жыл бұрын
What will be the complexity of memo solution
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
sir what if cooldown is of d days it is hard to visualise with states although very easy with recursive + memo
@techdose4u
@techdose4u 4 жыл бұрын
Yep. With d-days cooldown, you will stay at noStock state for atleast d-days when you reach there from Sold state.
@vishalmishra9549
@vishalmishra9549 4 жыл бұрын
@@techdose4u So like the present soldstate value be equal to past one and only nostock and inhand state will get changed?
@vishalmishra9549
@vishalmishra9549 4 жыл бұрын
@@techdose4u so the soldstate value will be const for d-1 days?
@devilkanu4890
@devilkanu4890 3 жыл бұрын
Can anyone explain how the state machine dynammic programming following the given cooldown constraint?
@vikasg5395
@vikasg5395 4 жыл бұрын
How do we reduce the space complexity from o(n) to just 3 variables? We need at least 3 rows x 2 columns. I am I missing something? (State diagram based sol)
@techdose4u
@techdose4u 4 жыл бұрын
You need total 4 variables to remeber state of sold. 3 rows 2 cols is also fine.
@vikasg5395
@vikasg5395 4 жыл бұрын
@@techdose4u thanks for the quick response
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@ankurrohilla4655
@ankurrohilla4655 4 жыл бұрын
sir how the conditions changes when the cooldown period is not 1 lets say it's 5?
@techdose4u
@techdose4u 4 жыл бұрын
Then try to identify where I used 1. You can simply change the recursion jump size. I have explained in the code, where to change.
@ankurrohilla4655
@ankurrohilla4655 4 жыл бұрын
@@techdose4u sir if we are going with second approach with cooldown period 5 ... we have to compare the current day scenario with the curr-5 days . sir , am I correct or not ?
@jayeshmarathe7678
@jayeshmarathe7678 3 жыл бұрын
Was the recursive code difficult to understand or its just me ☹️
@ashishg656
@ashishg656 4 жыл бұрын
Great :)
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
sir apne bilkul achcha nhi smjhaya is video me in comparison to al lyour other videos....kuchh samajh me nahi aya... what to do now?
@techdose4u
@techdose4u 4 жыл бұрын
Esko skip krdo. Baad mein samajh aa jaega. Eske pehle wale videos dekhlo.
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
dekh chuka hu...apne isme achcha nahi smjhaya h
@techdose4u
@techdose4u 4 жыл бұрын
Kaunse area ko achhe se nhi btaya bta do. Firr agli baar se usko improve krunga.
@SumitVerma-ln5nz
@SumitVerma-ln5nz 2 жыл бұрын
Me to my Father: Should we invest in crypto-currency? My Father: @27:37
@vaishalirathore3723
@vaishalirathore3723 3 жыл бұрын
how is the state approach taking cooldown into account?
@054_heenaahmed8
@054_heenaahmed8 2 жыл бұрын
in the state approach , in hand state is not followed by sold state, that is we are not buying stock immediately after selling it. Hence cooldown is taken into account.
Best Time to Buy and Sell Stock III | Leetcode #123
35:40
Techdose
Рет қаралды 72 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
Minimum edit distance | Dynamic programming | Backtracking
28:52
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 167 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 781 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 238 М.
DP 37. Buy and Sell Stocks III | Recursion to Space Optimisation
31:50
take U forward
Рет қаралды 186 М.
The Boundary of Computation
12:59
Mutual Information
Рет қаралды 1 МЛН
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 789 М.
DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥
9:11
take U forward
Рет қаралды 444 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41