Leetcode 309. Best Time to Buy and Sell Stock with Cooldown [ Algorithm + Code Explained]

Поделиться
HTML-код
  • Опубликовано: 8 окт 2024

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

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

    I had watched about 2/3 videos about this question until I cam across yours, and finally understood it! Thank you so much!

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

    Your explanation was really good. As far as constant space O(1) is concerned we can use state machine method to achieve it by using 3 variables for buy, sell & cooldown respectively.

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

    Thank you for the explanation! After you explained the high level logic, I didn't even need to see your code and passed all my tests (I usually need high level understanding unless I lack the technical knowledge)

  • @sarveshmukeshshahapurkar6203
    @sarveshmukeshshahapurkar6203 4 года назад +9

    One of the best videos I ever saw on this question !!

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

    Crisp explanation! Looking forward to more.

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

    Here is better solution: C#
    int sold = 0;
    int hold = Int32.MinValue;
    int rest = 0;
    for (int i = 0; i < prices.Length; i++)
    {
    int prevSold = sold;
    sold = hold + prices[i];
    hold = Math.Max(hold, rest - prices[i]);
    rest = Math.Max(rest, prevSold);
    }
    return Math.Max(rest, sold);

  • @jayatitiwari444
    @jayatitiwari444  4 года назад +15

    Hi All,
    Please find the time and space complexity of this solution below -
    Time complexity - O(n)
    Space Complexity - 0(n)
    **Here we are using 2D array (row = no. of elements in given array i.e n , column = 2 ) . So we can treat space complexity as O(n).**.
    Please comment if you can think a way to solve this question in constant space ?
    Also comment if you have any better way to solve this problem.

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

      maam shoudnot the dp[0][0] = prices[0], if not please explain, i am really cnfused please!!! thanks

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

      @@vibekdutta6539 how can you sell the stock at the 0th price on day 0?

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

      i get the answer of my question,0th day selling a stock without buying one aint ppossible

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

      I think we can optimize this one by getting rid of the dp vector since we need to consider last two states. This practice is standard and I'm pretty sure you are aware of keeping two variables instead of an entire vector for storing two states.
      Anyway, kudos to you.

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

      That was good... efficient code and well explained walk through.

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

    Thanks for sharing. Very detailed explanation.

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

    best explanation by far

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

    Thank you for nice explanation

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

    Beautiful explanation

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

    Good video. One small recommendation is to share the solution link that can viewed to revise them before the interviews

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

    Thanks for such an intuitive explanation

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

    Great explanation. Easy to understand. Thanks!

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

    Thank you! It is an awesome explanation!

  • @AmitKumar-we8dm
    @AmitKumar-we8dm 4 года назад

    bahaut badhiya ji... dil khush kar ditta aapne !!!

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

    Instead of just filling up the table in a bottom up manner , You should have explained the recursive formula , how it is working and build the solution in a top down approach as they are easy to understand.

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

    great explanation, thank you for sharing.

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

    Great explanation

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

    Very helpful, thank you

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

    Nicely done.

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

    Great video Jayati, One question here : Why did you consider the day 0 holding a stock as -prices[0]. I mean dp[0][1] = -prices[0]. Why do we need to have a negative value of the price?

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

      Hey Pankaj , since we bought the stock on day 0 ,and paid its price therefore it has to be subtracted from our profit.our profit on day 0 is 0 bucks , so 0 - price of stock on day 0 = -price[0]

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

      dp represents max profit.. so if we have stock on 0 th day we have just bought the stock .. hence what ever the profit we get in the future will be selling price - cost price ... cost price is now prices[0] ... selling price we don't know yet.. where for we add -prices[0] so in the future we need to only add the selling price to get profit...

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

    Best solution !!!

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

    This was helpful mam can you please do more questions on dp?

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

    Thank you! Amazing solution!

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

    Great explanation. Thank you!

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

    Nicely explained!

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

    Well explained

  • @刘恒-o7k
    @刘恒-o7k 3 года назад

    Bravo on this video!

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

    good video!

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

    Awesome got it thanks

  • @harsha.m4026
    @harsha.m4026 4 года назад +3

    We could do it with constant space

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

    Plz could you make a quesiton on regex solving in c++ using stl..

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

    But how do you make a conclusion it can be solved using DP ? Can you make a iteration approach first and then recursive and then come to a conclusion .... ?

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

    I think your explanation lacks some basic info. So, in nutshell, what values do dp[i][0] and dp[i][1] have? And why are we just returning dp[-1][0], not max(dp[-1])??

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

    wow, where have you been all this time?

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

    Hi, thanks for the video and I really like the clear explanation, but one thing I can't understand is the return value.
    why just return dp[len-1][0] instead of max{dp[len-1][0], dp[len-1][1]} ?
    How to guarantee that I always don't want to hold the stock on the last day? I may want to hold it on the last day if that gives me the best value?

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

      Buying stock means you will spend the money . So assume if on day i you have x amount and on i + 1 you buy stock then on that day you will have x - ( cost of that stock ) amount left with you . If i+1 is last day then you will never buy a stock as you don't have any day left to sell it. So to ensure maximum profit you should not have any stock unsold.

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

      @@jayatitiwari444 Eva said Thank you!

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

    9:22 you forget to handle the case when len == 2 && prices[0] == prices[1]

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

    kuch samj nai aya lol

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

    Bad explanation, no one could ever come up with that dp equation by himself directly. Should have been better if you had built the solution recursively and then applied memoization.After the recursive solution is build then only we can write the dp equation.

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

    Very poor explaing.