Best time to buy and sell stock 4 | Leetcode

Поделиться
HTML-код
  • Опубликовано: 19 окт 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...

Комментарии • 59

  • @yitingg7942
    @yitingg7942 3 года назад +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  3 года назад +2

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

    • @jeneyify
      @jeneyify 3 года назад +1

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

    • @yitingg7942
      @yitingg7942 3 года назад

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

  • @annawilson3824
    @annawilson3824 3 года назад

    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 3 года назад +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 3 года назад +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!!!

  • @rajatgupta-ld1wg
    @rajatgupta-ld1wg 3 года назад

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

  • @rajaryan7566
    @rajaryan7566 2 года назад +1

    you made this hard question so easy, Respect++

  • @mansoor.postbox
    @mansoor.postbox 3 года назад +1

    Fantastic explanation! Thank you so much.

  • @pareshpatil3060
    @pareshpatil3060 2 года назад +1

    The best explanation

  • @michaelchan6144
    @michaelchan6144 3 года назад

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

  • @deep.amrutiya
    @deep.amrutiya 4 года назад +1

    You are best. Great explanation

  • @vishalgoel6323
    @vishalgoel6323 4 года назад +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.

  • @denniscwang
    @denniscwang 3 года назад

    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?

  • @ramyfakhoury8767
    @ramyfakhoury8767 4 года назад +3

    This channel is awesome thank u

  • @dhanashreegodase4445
    @dhanashreegodase4445 2 года назад

    Thanks

  • @2121sourabh
    @2121sourabh 3 года назад +1

    great explanation

  • @annawilson3824
    @annawilson3824 3 года назад

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

  • @goswami46457
    @goswami46457 4 года назад

    Sir please make more videos on dynamic programming

  • @starc701
    @starc701 4 года назад +5

    this question was asked by oracle in first round

    • @techdose4u
      @techdose4u  4 года назад

      Nice to let everyone know :)

  • @buttercup5678
    @buttercup5678 3 года назад +2

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

  • @onkarbagal8268
    @onkarbagal8268 4 года назад +3

    Nice explanation ❤️

  • @anubhavaron718
    @anubhavaron718 3 года назад +1

    Killed it man❤️

  • @prettythings5572
    @prettythings5572 2 года назад

    n^2*k is bad solution for this ?

  • @stephenv9957
    @stephenv9957 4 года назад +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  4 года назад

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

    • @vishalgoel6323
      @vishalgoel6323 4 года назад

      Did you find your your mistake ?

    • @ayushgarg5929
      @ayushgarg5929 3 года назад

      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 4 года назад +1

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

    • @techdose4u
      @techdose4u  4 года назад +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 4 года назад

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

  • @User-sdjowije-92771
    @User-sdjowije-92771 Год назад

    it will be great if I can read the subtitles.

  • @prithvirajrathore4256
    @prithvirajrathore4256 4 года назад

    Sir can u make a series on codeforces qiestions?

    • @techdose4u
      @techdose4u  4 года назад

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

  • @mystryb345
    @mystryb345 4 года назад

    Sir could you cover leetcode substring concatenation

  • @maheshreddy4080
    @maheshreddy4080 3 года назад

    How to solve k transactions with cooldown

  • @pleasesirmorevideos4684
    @pleasesirmorevideos4684 4 года назад +2

    mastering iterative dp is tough

  • @Learner010
    @Learner010 4 года назад +1

    Graph Algo?

    • @techdose4u
      @techdose4u  4 года назад +1

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

  • @shikharchaudhary4925
    @shikharchaudhary4925 2 года назад

    This approach is not intuitive at all

  • @SuperMaDBrothers
    @SuperMaDBrothers 9 месяцев назад

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