Best time to buy and sell stock with cooldown | Leetcode #309

preview_player
Показать описание
This video explains the approach to solve the problem, best time to buy and sell stock with cooldown which is from leetcode 309.This is an extension for the problem best time to buy and sell stock 1 & 2.In this video,i have explained the approach to think about the solution for this problem and i have also shown how the given problem can be formulated as a recursion problem and how we can optimize it to solve the problem efficiently.In the second half of the video, i have explained the most efficient approach to solve this problem by making use of the states a person can be on a given day.There are only 3 states possible and so i have shown how the problem can be formulated into a state machine problem.This is the best approach which takes just O(N) time.This approach is also a dynamic programming approach.The space complexity can be reduced to O(1) because we just need to remember the previous day state a person can be.So, this can be done using 3 variables because we have just 3 possible states.At the end of the video, i have explained the code walkthrough of the given problem both by recursion + memoization and also state machine dynamic programming.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:

=======================================================================

USEFUL LINK:-
Рекомендации по теме
Комментарии
Автор

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😻🖤

PRANAVMAPPOLI
Автор

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 🙏

abhishekks
Автор

I was thinking of memoization technique but your state diagram method is much optimized. Loved it

yuvrajsinghrathore
Автор

this really made the concept crystal clear
thank as i always say ur channel is very underrated..

drishtdyumnshrivastava
Автор

This was so clear.Very well explained .Thank you so much.

sameekshabhatia
Автор

keep sharing your knowledge of problem solving SIR.It will be very helpful for us

trineshreddybs
Автор

Super explanation with great clarity. Learnt a new technique. Thanks!

BaishaliGhosh
Автор

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

piyushgoyal
Автор

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 ?

VarunMittal-viralmutant
Автор

Best illustration on three-state approach on youtube for this problem.

jaden
Автор

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

pleasesirmorevideos
Автор

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.

K_EC_AushoRoup
Автор

Great video 😇
Thank you so much to explain always in very deeply

kunalsoni
Автор

In the state diagram how are we considering the cool off days, what if the cool of day is more than 1 ?

anandbangad
Автор

Can anyone explain how the state machine dynammic programming following the given cooldown constraint?

devilkanu
Автор

awesome explanation both with dp and state diagram

sumonchatterjee
Автор

how is the state approach taking cooldown into account?

anushkagupta
Автор

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?

animeshjain
Автор

Wow what a explanation sir finally i understood all type problem of it..Sir if possible make video on at max K transactiom

mystryb
Автор

The state machine algorithm is incredible... Can't imagine how to come up an algorithm like this

junxuchen