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

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 พ.ย. 2024

ความคิดเห็น • 125

  • @akasnani3464
    @akasnani3464 4 ปีที่แล้ว +20

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

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

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

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

  • @Vendettaaaa666
    @Vendettaaaa666 4 ปีที่แล้ว +14

    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 TH-cam 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*

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

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

  • @suewang6662
    @suewang6662 2 ปีที่แล้ว

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

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

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

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

  • @shivanib4723
    @shivanib4723 3 ปีที่แล้ว

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

  • @phanindratube10
    @phanindratube10 2 ปีที่แล้ว

    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.

  • @monidipasarkar806
    @monidipasarkar806 4 ปีที่แล้ว +5

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

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

  • @anchalpandey3619
    @anchalpandey3619 3 ปีที่แล้ว

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

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

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

  • @palakmantry
    @palakmantry 4 ปีที่แล้ว +1

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

  • @varuntaneja7073
    @varuntaneja7073 3 ปีที่แล้ว

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

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

  • @sidhuparas
    @sidhuparas 3 ปีที่แล้ว

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

  • @manu-singh
    @manu-singh 4 ปีที่แล้ว

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

  • @ThePaullam328
    @ThePaullam328 ปีที่แล้ว

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

  • @redstarentertainment2621
    @redstarentertainment2621 2 ปีที่แล้ว

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

  • @hasanalic.n970
    @hasanalic.n970 4 ปีที่แล้ว

    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.

    • @KeertiPurswani
      @KeertiPurswani  4 ปีที่แล้ว +1

      So glad I could help you ❤️

  • @pavankalyankolisetty5823
    @pavankalyankolisetty5823 3 ปีที่แล้ว

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

    • @KeertiPurswani
      @KeertiPurswani  3 ปีที่แล้ว

      Thank you so much Pavan. Means a lot😇😇

  • @siwi_drake4703
    @siwi_drake4703 3 ปีที่แล้ว

    I like ur denim..which brand?

  • @tejaspradhan1185
    @tejaspradhan1185 4 ปีที่แล้ว +1

    Really well explained! Thanks a lot!

  • @RajSingh-dl2lk
    @RajSingh-dl2lk 3 ปีที่แล้ว

    Underrated channel

    • @KeertiPurswani
      @KeertiPurswani  3 ปีที่แล้ว

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

  • @suranjanmondal2349
    @suranjanmondal2349 3 ปีที่แล้ว

    Great explanation! Thanks a lot!

  • @growandlearndaily
    @growandlearndaily 2 ปีที่แล้ว

    you are really awesome explainer :)

  • @arjunkhanna1803
    @arjunkhanna1803 2 ปีที่แล้ว

    could not understand why maxDiff approach will work?

  • @chaitanya5885
    @chaitanya5885 3 ปีที่แล้ว +1

    please explain the other two variations mam

  • @ayushprakash3890
    @ayushprakash3890 4 ปีที่แล้ว +1

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

  • @tirthjayswal9895
    @tirthjayswal9895 3 ปีที่แล้ว

    Really good explenation

  • @AyushDasMyGooglePage
    @AyushDasMyGooglePage 4 ปีที่แล้ว

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

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

  • @shwetajha8745
    @shwetajha8745 3 ปีที่แล้ว +1

    Amazing ✨👏🏻

  • @RohitExclusive
    @RohitExclusive 4 ปีที่แล้ว

    Amazing Explanation. Keep it Up!

  • @mayankpatel6735
    @mayankpatel6735 3 ปีที่แล้ว +1

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

  • @arjr678
    @arjr678 4 ปีที่แล้ว

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

  • @himanshidixit6406
    @himanshidixit6406 4 ปีที่แล้ว

    Keep going...great explanations..🤗

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

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

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

  • @deepakbhardwaj9543
    @deepakbhardwaj9543 4 ปีที่แล้ว

    good explanation! Thanks!

  • @ashutoshmalguri
    @ashutoshmalguri 2 ปีที่แล้ว

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

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

  • @nidhigupta9909
    @nidhigupta9909 3 ปีที่แล้ว

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

  • @mandar.vaidya
    @mandar.vaidya 3 ปีที่แล้ว

    Can you also create in Hindi

  • @vishalkumar-bz5mq
    @vishalkumar-bz5mq 4 ปีที่แล้ว +1

    great explanation , keep it up :)

  • @mahipalsingh-yo4jt
    @mahipalsingh-yo4jt 4 ปีที่แล้ว

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

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

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

    • @coppercu9335
      @coppercu9335 3 ปีที่แล้ว

      you are the problem.

  • @misubgify
    @misubgify 3 ปีที่แล้ว

    Once again, You should A+ for your explanations

  • @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  4 ปีที่แล้ว

      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

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

  • @theuntoldtree
    @theuntoldtree 3 ปีที่แล้ว

    ig ,i saw her on linkedin O.o

  • @vinaykumardahiya4260
    @vinaykumardahiya4260 4 ปีที่แล้ว

    amazing!

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

  • @bishnuagrawal828
    @bishnuagrawal828 3 ปีที่แล้ว

    it was helpful :)

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

  • @be_like__prateek
    @be_like__prateek 4 ปีที่แล้ว

    Rocked !!!

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

  • @stevemiller7050
    @stevemiller7050 11 หลายเดือนก่อน

    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.

  • @iamdeepnavi3438
    @iamdeepnavi3438 4 ปีที่แล้ว

    thank you mam