Best time to buy and sell stock 4 | Leetcode

Sdílet
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...

Komentáře • 59

  • @yitingg7942
    @yitingg7942 Před 3 lety +18

    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 😂

    • @techdose4u
      @techdose4u  Před 3 lety +2

      It's commendable that you are trying so hard 😊 You will get it soon.

    • @jeneyify
      @jeneyify Před 3 lety +1

      I'm trying so hard to actually understand even after seeing the solution.

    • @yitingg7942
      @yitingg7942 Před 3 lety

      @@jeneyify It is very hard, don't beat yourself up for that.

  • @annawilson3824
    @annawilson3824 Před 2 lety

    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.

  • @gangli3484
    @gangli3484 Před 3 lety +3

    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.

  • @gkn7104
    @gkn7104 Před 3 lety +1

    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!!!

  • @rajaryan7566
    @rajaryan7566 Před 2 lety +1

    you made this hard question so easy, Respect++

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg Před 3 lety

    Sir each and every time I see your video, I get impressed with the clear and crisp explanation. Thanks a lot :)

  • @mansoor.postbox
    @mansoor.postbox Před 3 lety +1

    Fantastic explanation! Thank you so much.

  • @deep.amrutiya
    @deep.amrutiya Před 3 lety +1

    You are best. Great explanation

  • @pareshpatil3060
    @pareshpatil3060 Před 2 lety +1

    The best explanation

  • @starc701
    @starc701 Před 4 lety +5

    this question was asked by oracle in first round

  • @buttercup5678
    @buttercup5678 Před 3 lety +2

    thankyou so much 😭😭😭😭😭😭😭 i really mean it

  • @onkarbagal8268
    @onkarbagal8268 Před 4 lety +3

    Nice explanation ❤️

  • @ramyfakhoury8767
    @ramyfakhoury8767 Před 4 lety +3

    This channel is awesome thank u

  • @2121sourabh
    @2121sourabh Před 3 lety +1

    great explanation

  • @vishalgoel6323
    @vishalgoel6323 Před 3 lety +1

    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.

  • @goswami46457
    @goswami46457 Před 4 lety

    Sir please make more videos on dynamic programming

  • @anubhavaron718
    @anubhavaron718 Před 3 lety +1

    Killed it man❤️

  • @denniscwang
    @denniscwang Před 3 lety

    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?

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 2 lety

    Thanks

  • @prettythings5572
    @prettythings5572 Před 2 lety

    n^2*k is bad solution for this ?

  • @User-sdjowije-92771
    @User-sdjowije-92771 Před rokem

    it will be great if I can read the subtitles.

  • @annawilson3824
    @annawilson3824 Před 2 lety

    Do we really need to allocate 2k+1 elements and go up to j=2k, if in reality we return 2k-1-th element?

  • @michaelchan6144
    @michaelchan6144 Před 3 lety

    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]??

  • @pleasesirmorevideos4684
    @pleasesirmorevideos4684 Před 4 lety +2

    mastering iterative dp is tough

  • @stephenv9957
    @stephenv9957 Před 3 lety +1

    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

    • @techdose4u
      @techdose4u  Před 3 lety

      Please ask this in telegram group for better and quicker help.

    • @vishalgoel6323
      @vishalgoel6323 Před 3 lety

      Did you find your your mistake ?

    • @ayushgarg5929
      @ayushgarg5929 Před 3 lety

      Here is shortest code:-
      int maxProfit(int k, vector& prices) {
      int n=prices.size();
      k=min(k,n/2);
      if(n

  • @ashutosh.sharma
    @ashutosh.sharma Před 4 lety +1

    Can we make 3D memoization table into a 2D table?

    • @techdose4u
      @techdose4u  Před 4 lety +1

      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.

    • @ashutosh.sharma
      @ashutosh.sharma Před 4 lety

      @@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?

  • @maheshreddy4080
    @maheshreddy4080 Před 3 lety

    How to solve k transactions with cooldown

  • @mystryb345
    @mystryb345 Před 4 lety

    Sir could you cover leetcode substring concatenation

  • @Learner010
    @Learner010 Před 4 lety +1

    Graph Algo?

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Graph algo is going on simultaneously. Don't worry.

  • @prithvirajrathore4256
    @prithvirajrathore4256 Před 4 lety

    Sir can u make a series on codeforces qiestions?

    • @techdose4u
      @techdose4u  Před 4 lety

      It will be very late. I am focussing on preparing for placements for now.

  • @shikharchaudhary4925
    @shikharchaudhary4925 Před 2 lety

    This approach is not intuitive at all

  • @SuperMaDBrothers
    @SuperMaDBrothers Před 7 měsíci

    This approach sucks. It's easy if you use automata