Coding Interview Question | Best time to Buy and Sell Stock | One, any number and max K Transactions

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

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

  • @akasnani3464
    @akasnani3464 4 года назад +19

    Your explaining power and clarity of thought is excellent. Keep it up.

  • @Vendettaaaa666
    @Vendettaaaa666 4 года назад +13

    I have a challenge for you Keerti, almost every solution in the internet talks about solving it iteratively. It doesn't explain how we all arrived at this iterative approach. Iterative approach is the last step in a DP solution. We first start with plain Recursion + Divide and Conquer, then see overlapping problems, then we memoize it, then we see that our problem's state changes on only two variables and we "transform" it into a iterative one.
    Unfortunately directly explaining the iterative approach is like we need to "memorize" this approach. I am looking for the INTUITION behind it.
    Thanks for taking the effort in making this video, I have some take aways from it.
    Looking forward to your response.
    Thank you,
    A fan

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

      Hi, thanks for your comment!
      I try to discuss the intuition behind each problem.
      Please do tell me what exactly did I miss. I would love to take up the challenge. I hope you have gone through the other dp videos as well :)
      Last thing I want someone to do through my videos is to memoize the solution..

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

      You definitely explained the intuition behind the iterative approach.
      But the thing is, the iterative approach is a FINAL step in DP optimization.
      It goes from Top Down (Recursive) ----> Then Memoize overlapping sub-problems ----> Then flip it upside down via bottom UP (Here there is no call stack, it's all iterative)
      It's hard to explain on RUclips comments, but I hope we are getting somewhere on the same page?

    • @KeertiPurswani
      @KeertiPurswani  4 года назад +4

      Yes I am getting what you are saying but in an interview you don't have so much time. Getting the Intuition and being able to write dp solutions is my core purpose for the series.
      If many people need it, I will surely make separate videos to cover memoization etc. Can't include in same video as most people prefer shorter videos and my videos are already long. I hope you understand what I am saying..

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

      A similar discussion happened on my wildcard matching video :)
      I am trying to follow approach that will help more number of people.
      Thank you for your feedback! If there are more people who need this, I will definitely make videos covering it :)

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

      @@KeertiPurswani Thank you Keerti *thumbs up*

  • @safakhan8177
    @safakhan8177 3 года назад +6

    Keerti, thank you so much for the best ever explanation! I finally understood the problem and was able to write the code. I have had trouble understanding the dynamic approach for k transactions. I tried various other explanations but I still couldn't figure it out. So glad to have found your channel!

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

    Very helpful explanation, really helped to start from the simpler problems and work the logic to the trickiest one! Love the support you are giving to all of us while explaining really hard moments! Thank you SO MUCH for your hard work!! Keep it up!

  • @monidipasarkar806
    @monidipasarkar806 3 года назад +5

    Thanks Keerti. Done an amazing job here :) Keep this up!

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

    Good explanation. In case of 1, 2, 3, 4, 5, 6 the profit of buying at 1 and selling at 6 is highest as we cannot buy and sell on the same day hence scenario of buying at 1 and selling at 2 and again buying at 2 and selling at 3 and so on is invalid. In this case peak and valley makes sense to maximize the profit, 1 is valley and 6 is peak.

  • @sanyamgirdhar6636
    @sanyamgirdhar6636 3 года назад +3

    As simple as it can be, beautiful explanation. The same approach works for all variants. Can you take some more standard hard problems?

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

    This was about finding Maximum Profit. How to find Minimum Loss that the user can have?

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

    could not understand why maxDiff approach will work?

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

    this question ist so dumb. in the Stockmarket is no Time Machine to see what the price will be in the future. so you just hope to buy low, but you will never ever know if you sell with the highest profit.

  • @ThePaullam328
    @ThePaullam328 Год назад

    How do you come up with "maxDiff"? How did you derive maxDiff from m -> 0...j - 1?

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

    They only thing that went into my head is that you are making really good profits in stocks...O(paisa^paisa)

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

    @Keerti, really nice explanation, keep doing more (variant - stock buy/sell
    with transaction fee).
    2 suggestions:
    - intuitive variable names (arr -> prices, M->profit, 'M' vs 'm' at 16:29 how about 'm'->k to keep it simple)
    - Please add git repo link for the solution and quick code walk-thru (2-3 min)

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

      Will do! Thanks for your suggestion 😊

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

    Didi code bhi daal dete to acha hota !!! I can see a million subscriber you tuber coming up ;)

  • @manishchikoti3658
    @manishchikoti3658 Год назад

    Am I only the one who thinks she could have explained better , well I learnt something so I do not have any regrets seeing this video

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

    please explain the other two variations mam

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

    I know this is a problem but in the real world, it's not possible because no one knows tomorrow's price. 😊

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

    ig ,i saw her on linkedin O.o

  • @mandar.vaidya
    @mandar.vaidya 3 года назад

    Can you also create in Hindi

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

    And please make videos for the other two variations as well. It will be really helpfull

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

    you are really awesome explainer :)

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

    Amazing ✨👏🏻

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

    Wouldn't it be k>=n/2 ??

  • @phanindratube10
    @phanindratube10 Год назад

    Liked the video. Would have been more intuitive if started with explaining the relevance of overlapping sub-prob and optimal sub-structure in this problem. I understand from comment section you are against doing memoization in the interviews but jumping directly to explaining optimised approach has high chance of people memoizing the solution itself :-) and find it hard to arrive at an approach when the question is tweaked in the interview. As with length of the videos, timestamps in the video description may help, if people wants to jump directly to final optimized solution. My dumb brain lost it when you started doing k=2. LOL. But still a great video. Myself a big fan of your catalan numbers video. Thanks for your efforts.

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

    Great explanation.. Keep posting such amazing video. Thanks a lot

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

      Thank you ❤️ Hope you like other videos as well 😇

  • @Adityasingh-ev3dg
    @Adityasingh-ev3dg 3 года назад

    well, at 7:12 you are saying we don't need to check that but that's wrong completely.You use else in place of else if it will be wrong bcoz its not necessary that arr[i]-min>profit always.

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

    Wooah!!! Your explanation was awesome it went right into my head!!!! Thankyou

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

    Found best explanation of this LeetCode question - out of all the videos of max K variation, this is by far the clearest.

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

    you might be bored reading this, but its my responsibility to acknowledge the help you did by making a video on this, thank you so much.

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

    This helps a lot. A very good compilation.Thanks Keerti!!

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

    The best video so far I have seen for Buy and Sell Stock problems. Thank you so much!

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

    Omg such a nice explanation :)
    This video is damn underrated!

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

    Opps ... I thought its a stock market tutorial to buy and sell ... 🙄 it's different

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

      🤭🤭
      In case you want to check out content on stocks, my father has a channel on the same with the name - Kishore Purswani 😇😇

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

    I like ur denim..which brand?

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

    Very nice explanation you covered all the scenario . Also no offense but its sometimes difficult to concentrate between you and the problem.

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

    Really well explained! Thanks a lot!

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

    I think maxdiff should be M[i][m]-v[m] instead of M[i-1][m]-v[m] because former is giving correct answer while later is giving incorrect answer. plz see into it ma'am.

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

      Depends on the question you are doing Divyanshu. If you can make buy after selling on the same day, it will be M[I][m]. If you can buy only one day after selling, it's M[I-1][m]

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

      @@KeertiPurswani okay ma'am got it. Thanx for replying. Your videos are really helpful.

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

    Awesome. One video for all cases is a gold resource. Great explanations :)

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

    Hii @Keerti Purswani
    thank u for such a beautifull solution with great explaination ........... looking forward for the videos on Best time to Buy and Sell Stock with cool down and with transaction fee . I solved these questions but still many times confuse , I hope i can get some clear clarity with your beautifull way of explaining :)

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

    Great explanation! Thanks a lot!

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

    It was an awesome explanation. But I have one doubt
    GFG code snippet:
    profit[t][i] = max(profit[t][i-1], price[i] + max(prevDiff, profit [t-1][i-1] - price[i-1])
    where prevDiff is max(profit[t-1][j] - price[j]) for all j in range [0, i-2]
    This thing I didn't understand, profit [t-1][i-1] - price[i-1]. Can someone clear my doubt like why profit[t-1][i-1] - price[i-1].

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

      Abe tujhse kisi ne pucha ki code de faltu ka hero ban raha hai lodu😡😡😡

    • @techdemy4050
      @techdemy4050 3 года назад +3

      @@siwi_drake4703 Homie chill, I know your parents might use these words at your home but dont show your culture what your parent's taught you here in social media it gets viral dude.

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

      It's like that , because of the formula , we need max of those values for optimization. Watch video again.

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

    Really good explenation

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

    Thanks for explanation.I have been going through many videos for this problem.But still I'm struggling for cool down.Can you explain it?

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

    I watched all videos on this question but your explanation was the best among them. Keep up the awesome work!. It really helped me. Thank you for taking your time out and teaching us.

  • @vishalkumar-bz5mq
    @vishalkumar-bz5mq 4 года назад +1

    great explanation , keep it up :)

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

    Ye ques maine bohot pehle try kiya tha pr nhi huwa but aaj ek aur try diya but I was'nt able to solve.
    Finally I got the approch.
    Thanks for your effort

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

    Hello Keerti! Is there any way we can apply this optimization (maxDiff) to a recursive solution? I am more into recursive based dynamic programming solutions as I find them much more intuitive and it has paid me dividends! I was finding it difficult to implement this optimization in a recursive solution.

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

    Just Loved the Explanation, Wanted more tutors like you :)

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

      Thank you so much Pavan. Means a lot😇😇

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

    Are we allowed to buy and sell on the same day?? I mean i bought the stock on day x @ 1 sold it @ day y @ 5 again buy the stock @ 5 and sold it on some day z @ 10 ??
    Its like: buy (sell,buy) sell ... We are selling and then buying on the same day.. . is this allowed ... Because the question mentions it is not allowed but ur algorithm allows this... Help me !

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

      Hi Ayush,
      You can buy a stock on the same day after selling it
      Otherwise it would have been mentioned that there is a cool down of 1 day and you can buy on next day only 😊

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

      @@KeertiPurswani This doesn't seem right, why we will buy and sell the same day?

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

      @@ajaysharma2007ym sell the previous stock and buy a new stock

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

    best video explanation on yt for this problem

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

      Thanks Rohit. Hope you like rest of the videos as well 😇

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

    Quite enlightening! Can some one please provide this array for the future of any one company.

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

    Once again, You should A+ for your explanations

  • @hasanalic.n970
    @hasanalic.n970 3 года назад

    Thanks, a lot keerti, God Bless you. I was struggling to understand this problem from morning to evening I was not getting into my head. Finally, your explanation and way of teaching helped me to understand clearly without any doubts. Thanks a lot once again.

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

    Amazing Explanation. Keep it Up!

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

    And I wanna ask something. Without knowing the problem from basic like recursion it's waste of learning. It's mostly like byhearting the problem. In interviews we can't explain clearly..My request is to make videos from basic

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

      I have been explaining the logics. I hope noone thinks that is anywhere near to "byhearting" the problem. But yes, you are right and I am planning to cover recursion in coming videos 😊 My major worry initially was the length of videos. Already my videos are pretty long and I am sure if I add recursion, many people won't open them because everyone is short of time 😅

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

      @@KeertiPurswani
      Make the one video into parts and complete topic covers nearly 3-4 problems in leetcode. So it'll be very helpful if we understand from basics

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

      @@ravitejachittela4370 this is the only video where I have covered multiple problems. Please checkout other dp problems on my channel as well. Even those videos are pretty long 😅

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

      @@KeertiPurswani okay..your explanation is good. Once again thanks for making videos for us

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

      Thank you @raviteja. It really means a lot to me ☺️ and I hope you didn't misunderstand what I was saying. I genuinely want people to understand the questions I do tutorials on 😅

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

    Next level explanation .....Hats off!!

  • @manu-singh
    @manu-singh 3 года назад

    I finally got my head around this problem today.
    All thanks to you Keerti di.

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 3 года назад

    nice explanation !!!!!!!!!!!!!

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

    it was helpful :)

  • @RajSingh-dl2lk
    @RajSingh-dl2lk 3 года назад

    Underrated channel

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

      Please do help the channel grow by sharing it with your friends 😊

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

    Keep going...great explanations..🤗

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

    GREAT EXPLANATION !!!!!!!!!!!

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

    good explanation! Thanks!

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

    Rocked !!!

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

    Make variations of stock sell problem vidoes

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

      Which variations?

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

      @@KeertiPurswani one with cooldown , it's there on leetcode

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

      Yes I have mentioned that in the end of video. One is with cooldown and one with transaction fees :)

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

    amazing!

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

    thank you mam