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😻🖤
@theghostwhowalk4 жыл бұрын
Liked the State diagram way!
@techdose4u4 жыл бұрын
I personally liked it too. So, I skipped tabulation DP approach.
@abhishekks67824 жыл бұрын
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 🙏
@techdose4u4 жыл бұрын
Welcome bro :)
@vvk62604 жыл бұрын
@@techdose4u who came up with this, that is amazing..
@siddharthsaxena64953 жыл бұрын
great explanation
@techdose4u3 жыл бұрын
Thanks
@yuvrajsinghrathore49193 жыл бұрын
I was thinking of memoization technique but your state diagram method is much optimized. Loved it
@drishtdyumnshrivastava53132 жыл бұрын
this really made the concept crystal clear thank you...... as i always say ur channel is very underrated..
@jaden25824 жыл бұрын
Best illustration on three-state approach on youtube for this problem.
@techdose4u4 жыл бұрын
Thanks :)
@K_EC_AushoRoup4 жыл бұрын
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.
@techdose4u4 жыл бұрын
Nice 😊
@trishulcurtis18104 жыл бұрын
Whats the time complexity of recursive memoization ?
@anandbangad29664 жыл бұрын
In the state diagram how are we considering the cool off days, what if the cool of day is more than 1 ?
@sharmamukul9383 жыл бұрын
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-viralmutant3 жыл бұрын
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 ?
@karankhara13122 жыл бұрын
Thanks for the great explanation!
@techdose4u2 жыл бұрын
Welcome 😊
@BaishaliGhosh134 жыл бұрын
Super explanation with great clarity. Learnt a new technique. Thanks!
@techdose4u4 жыл бұрын
Welcome :)
@rishabhgoyal81104 жыл бұрын
God bless you Man!
@techdose4u4 жыл бұрын
❤️
@junxuchen28514 жыл бұрын
The state machine algorithm is incredible... Can't imagine how to come up an algorithm like this
@techdose4u4 жыл бұрын
It's very difficult 😅
@surabhisingh99563 жыл бұрын
Wow, such a good explanation
@techdose4u3 жыл бұрын
Thanks :)
@code_life48354 жыл бұрын
Great Explanation! Thanks a lot, Sir
@techdose4u4 жыл бұрын
Welcome
@suyashlokhande19813 жыл бұрын
simply awsome
@techdose4u3 жыл бұрын
Thanks
@piyushgoyal96333 жыл бұрын
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
@tushardhote63934 жыл бұрын
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.
@techdose4u4 жыл бұрын
Try to seebthe memoization + recursion code for cooldown. I have explained where you need to make change to solve for D-days.
@tushardhote63934 жыл бұрын
@@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?"
@pleasesirmorevideos46844 жыл бұрын
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
@techdose4u4 жыл бұрын
Okay sure
@andriidanylov9453 Жыл бұрын
Great job. Appreciate
@sumonchatterjee53374 жыл бұрын
awesome explanation both with dp and state diagram
@techdose4u4 жыл бұрын
Thanks
@harshitsharma78713 жыл бұрын
Very well explained. Appreciate it.
@neerajchoudhary37093 жыл бұрын
bhai bhai, kya baat hai, thanks man
@trineshreddybs8424 жыл бұрын
keep sharing your knowledge of problem solving SIR.It will be very helpful for us
@prateeksinghal6304 жыл бұрын
I loved the memoized code !!
@techdose4u4 жыл бұрын
Thanks :)
@anushkagupta59174 жыл бұрын
how is the state approach taking cooldown into account?
@shushantgaur94204 жыл бұрын
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?
@techdose4u4 жыл бұрын
You should watch the next version of this problem and my solution
@shushantgaur94204 жыл бұрын
@@techdose4u all right👍. Although I was going to watch it anyways, cause it's really good content :) Thanks again
@techdose4u4 жыл бұрын
@@shushantgaur9420 nice :)
@shaziasamreen85844 жыл бұрын
Great explaination 👏
@techdose4u4 жыл бұрын
Thanks :)
@mystryb3454 жыл бұрын
Wow what a explanation sir finally i understood all type problem of it..Sir if possible make video on at max K transactiom
@techdose4u4 жыл бұрын
Sure :)
@sameekshabhatia32003 жыл бұрын
This was so clear.Very well explained .Thank you so much.
@rishabh20553 жыл бұрын
Good insightful Explanation 👍
@ankitgoyal344 жыл бұрын
Thank you for explanation!!
@techdose4u4 жыл бұрын
Welcome :)
@slimysnail6071 Жыл бұрын
What is the time complexity for the recursion approach?
@VivekMishra-hd7mg4 жыл бұрын
very well explained
@techdose4u4 жыл бұрын
Thanks
@amchourasia3 жыл бұрын
Perfect
@techdose4u3 жыл бұрын
Thanks :)
@firstjm907117 күн бұрын
Thank you!
@techdose4u17 күн бұрын
welcome :)
@666Imaginative2 жыл бұрын
thank you so much sir
@mishikaraj51534 жыл бұрын
well explained Sir!
@techdose4u4 жыл бұрын
Thanks :)
@amitavamozumder733 жыл бұрын
please make a video on how to identify state machine problems.
@techamet4284 жыл бұрын
like usual.. beautifully explained 🔥🔥
@techdose4u4 жыл бұрын
Thanks :)
@kunalsoni76814 жыл бұрын
Great video 😇 Thank you so much to explain always in very deeply
@techdose4u4 жыл бұрын
Welcome :)
@prasad.patil124 жыл бұрын
Great explanation 👍
@vishalverma9202 жыл бұрын
what is the time complexity of recursive solution after memoization ??
@pramodsinghyadav75803 жыл бұрын
great explanation.I was looking for memoziation method which code is explained here nicely
@techdose4u3 жыл бұрын
Thanks
@deepakpoojari90194 жыл бұрын
Thanks man
@techdose4u4 жыл бұрын
Welcome
@animeshjain45943 жыл бұрын
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?
@shubhamgiri28404 жыл бұрын
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-pg8kx4 жыл бұрын
What if cooldown period is 2 days (n days)
@financewithsom4854 жыл бұрын
the 2nd method is only for cooldown of 1 day right??
@techdose4u4 жыл бұрын
Yes. If you can modify the state machine then it may also work for K days :)
@lobsanggyatso62804 жыл бұрын
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
@techdose4u4 жыл бұрын
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.
@kunalsoni76814 жыл бұрын
@@techdose4u thanking you 😇
@prateeksinghal6304 жыл бұрын
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 !!
@mvee4 жыл бұрын
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); } }
@techdose4u4 жыл бұрын
Thanks
@shushantgaur94204 жыл бұрын
@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.
@mvee4 жыл бұрын
@@shushantgaur9420 ohh, I see what you mean. Thank you for the clarification!
@shushantgaur94204 жыл бұрын
@@mvee no problem bro:)
@gauravagarwal85923 жыл бұрын
Why you say K+1 instead of K for the K cool Down period ? Can someone explain please?
@shahperzia21113 жыл бұрын
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
@gauravagarwal85923 жыл бұрын
@@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.
@shahperzia21113 жыл бұрын
@@gauravagarwal8592 yes for k=1 with can buy i+2 th day
@subhamtripathi19973 жыл бұрын
sorry , but didnt we skip the "2" in the second valley?
@amanrai80104 жыл бұрын
What will be the complexity of memo solution
@pleasesirmorevideos46844 жыл бұрын
sir what if cooldown is of d days it is hard to visualise with states although very easy with recursive + memo
@techdose4u4 жыл бұрын
Yep. With d-days cooldown, you will stay at noStock state for atleast d-days when you reach there from Sold state.
@vishalmishra95494 жыл бұрын
@@techdose4u So like the present soldstate value be equal to past one and only nostock and inhand state will get changed?
@vishalmishra95494 жыл бұрын
@@techdose4u so the soldstate value will be const for d-1 days?
@devilkanu48903 жыл бұрын
Can anyone explain how the state machine dynammic programming following the given cooldown constraint?
@vikasg53954 жыл бұрын
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)
@techdose4u4 жыл бұрын
You need total 4 variables to remeber state of sold. 3 rows 2 cols is also fine.
@vikasg53954 жыл бұрын
@@techdose4u thanks for the quick response
@techdose4u4 жыл бұрын
Welcome
@ankurrohilla46554 жыл бұрын
sir how the conditions changes when the cooldown period is not 1 lets say it's 5?
@techdose4u4 жыл бұрын
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.
@ankurrohilla46554 жыл бұрын
@@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 ?
@jayeshmarathe76783 жыл бұрын
Was the recursive code difficult to understand or its just me ☹️
@ashishg6564 жыл бұрын
Great :)
@techdose4u4 жыл бұрын
Thanks
@abhinavmishra76174 жыл бұрын
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?
@techdose4u4 жыл бұрын
Esko skip krdo. Baad mein samajh aa jaega. Eske pehle wale videos dekhlo.
@abhinavmishra76174 жыл бұрын
dekh chuka hu...apne isme achcha nahi smjhaya h
@techdose4u4 жыл бұрын
Kaunse area ko achhe se nhi btaya bta do. Firr agli baar se usko improve krunga.
@SumitVerma-ln5nz2 жыл бұрын
Me to my Father: Should we invest in crypto-currency? My Father: @27:37
@vaishalirathore37233 жыл бұрын
how is the state approach taking cooldown into account?
@054_heenaahmed82 жыл бұрын
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.