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

Поділитися
Вставка
  • Опубліковано 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)

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

  • @sarveshmukeshshahapurkar6203
    @sarveshmukeshshahapurkar6203 4 роки тому +9

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

  • @ashokb265
    @ashokb265 4 роки тому +2

    Thank you for nice explanation

  • @barathnatarajan8566
    @barathnatarajan8566 2 роки тому

    Beautiful explanation

  • @IsanSahoo
    @IsanSahoo 4 роки тому +2

    Crisp explanation! Looking forward to more.

  • @AmitKumar-we8dm
    @AmitKumar-we8dm 4 роки тому

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

  • @dinakaranragunathan7721
    @dinakaranragunathan7721 2 роки тому

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

  • @beteltadesse9803
    @beteltadesse9803 2 роки тому

    Thank you! It is an awesome explanation!

  • @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.

  • @enweiwu2361
    @enweiwu2361 3 роки тому

    Great explanation. Easy to understand. Thanks!

  • @sushantchoudhary102
    @sushantchoudhary102 4 роки тому +1

    Thanks for such an intuitive explanation

  • @mashab9129
    @mashab9129 3 роки тому

    great explanation, thank you for sharing.

  • @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...

  • @nadaralpenidze9549
    @nadaralpenidze9549 4 роки тому +1

    Very helpful, thank you

  • @winning_aadict
    @winning_aadict 4 роки тому +1

    Nicely done.

  • @bibhu_pala
    @bibhu_pala 3 роки тому

    Nicely explained!

  • @gauravmadan5217
    @gauravmadan5217 4 роки тому +1

    Great explanation

  • @pallabiroy9091
    @pallabiroy9091 3 роки тому

    Best solution !!!

  • @ilfat_khairullin
    @ilfat_khairullin 3 роки тому

    Thank you! Amazing solution!

  • @leejsom3605
    @leejsom3605 3 роки тому +1

    good video!

  • @anuragsekhri2315
    @anuragsekhri2315 4 роки тому +1

    Well explained

  • @刘恒-o7k
    @刘恒-o7k 3 роки тому

    Bravo on this video!

  • @adityasaibewar662
    @adityasaibewar662 4 роки тому +1

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

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

  • @abhaypatil2000
    @abhaypatil2000 4 роки тому

    Awesome got it thanks

  • @senthamilsindhu901
    @senthamilsindhu901 4 роки тому +1

    Great explanation. Thank you!

  • @Modernpunjabifamily
    @Modernpunjabifamily 4 роки тому

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

  • @harsha.m4026
    @harsha.m4026 4 роки тому +3

    We could do it with constant space

  • @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]

  • @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])??

  • @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.