DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥

Sdílet
Vložit
  • čas přidán 14. 03. 2022
  • Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
    Problem Link: bit.ly/3rN7GIL
    Please watch the video at 1.25x for a better experience.
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/takeUforward
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the first problem on DP on Stocks. Its the Best time to buy and sell stocks.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
    You can also get in touch with me at my social handles: linktr.ee/takeUforward

Komentáře • 436

  • @takeUforward
    @takeUforward  Před 2 lety +122

    Please give us a like and share this content as much as possible :)

    • @lapimpale
      @lapimpale Před 2 lety

      Thank you bro for your hard work to add new videos. Congratulations for completing 2 years on youtube :)

    • @rechinraj111
      @rechinraj111 Před 2 lety

      When will remaining DP problems will come ?

  • @avishkarpatil5871
    @avishkarpatil5871 Před 5 měsíci +18

    8:36
    The provided solution is not using dynamic programming. Dynamic programming involves breaking down a problem into smaller subproblems and solving each subproblem only once, storing their solutions to avoid redundant calculations.
    The solution provided is a simple linear scan through the prices array, keeping track of the minimum price encountered so far (temp) and updating the maximum profit (maxi) accordingly. It doesn't involve breaking down the problem into subproblems or utilizing memorization of intermediate results, which are characteristic features of dynamic programming algorithms.
    Instead, it employs a straightforward greedy approach to find the maximum profit by considering the difference between each price and the minimum price encountered so far.

    • @_AmbujJaiswal
      @_AmbujJaiswal Před 2 měsíci +2

      true.. it greedy works best for this question

    • @saatvikmangal7994
      @saatvikmangal7994 Před 26 dny +1

      but Isn't it dividing into sub-problems as well if we carefully observe
      The for loop at the time of the ith iteration will answer up to that ith iteration.
      But you are correct in the sense that we are not leveraging this answer to find the next answer, however, we are leveraging minimum value to find the next answer

    • @priyanshkumariitd
      @priyanshkumariitd Před 24 dny

      Yes, I agree with you

  • @TW-uk1xi
    @TW-uk1xi Před 2 lety +212

    Because of this guy, I love dynamic programming.

    • @rahulbhagat4023
      @rahulbhagat4023 Před 2 lety +34

      Yes u will until u start solving different questions

    • @ShubhamVerma-hw4uj
      @ShubhamVerma-hw4uj Před 2 lety +3

      @@rahulbhagat4023 so these questions are not enough?

    • @priyanshumohanty5261
      @priyanshumohanty5261 Před 2 lety +10

      @@rahulbhagat4023 Everyone starts somewhere ig. Many people found these problems ridiculously hard earlier

    • @nanda_8
      @nanda_8 Před rokem +17

      Codeforces me dp tag lagake 1800 rating laga..
      Sara bhukar utar jayega :}
      BTW no harm to striver bhaiya... He is explaining all the classical problems using which we can solve hard problems with sufficient practice.
      Watching this whole play list is not at all sufficient. (Hard work from your end is needed)

    • @ShubhamVerma-hw4uj
      @ShubhamVerma-hw4uj Před rokem +4

      @@nanda_8 yha 1500 nhi hore h bhai tu 1800 ki baat kr ra hai

  • @LowkeyCoder
    @LowkeyCoder Před 8 měsíci +7

    maan gye guru! this was my first video here and explnation was very good!

  • @stith_pragya
    @stith_pragya Před 6 měsíci +2

    UNDERSTOOD.....Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @RAKSHITHPGBBTCSBTechCSE
    @RAKSHITHPGBBTCSBTechCSE Před 9 měsíci +5

    I think u are the best teacher in this whole world...keep it up man...

  • @johncenakiwi
    @johncenakiwi Před 2 lety +10

    Thanks Striver, I have been stuck on the Buy and Sell stocks with at most k transactions problem for sometime now. Will wait for your video.

  • @Hrushi_2000
    @Hrushi_2000 Před 10 měsíci +1

    Understood. Thankyou Sir

  • @samuelfrank1369
    @samuelfrank1369 Před 9 měsíci +1

    Understood. Thanks a lot. Please upload more videos Bhaiyaaa

  • @torishi82
    @torishi82 Před 18 dny

    Nobody explains like you. Awesome.

  • @shashankjagtap5051
    @shashankjagtap5051 Před rokem +3

    There are many ways of solving this problem, but your one is the best.!

  • @arkaprabhasaha8203
    @arkaprabhasaha8203 Před 2 měsíci +1

    Following your entire DSA A2Z Course!

  • @UECAshutoshKumar
    @UECAshutoshKumar Před měsícem +1

    Thank You!
    Understood

  • @23lifeisgod
    @23lifeisgod Před 29 dny

    Understood! thanks !! Long live !!!

  • @rocktatnine
    @rocktatnine Před 9 měsíci +5

    Hello, thanks for this solution. Lucid & perfectly explained. I have a simple doubt. How does this problem fall in the category of DP. I'm genuinely confused coz my understanding of DP is different. Pls Help.

  • @konankikeerthi
    @konankikeerthi Před měsícem

    Thank you bro. Understood

  • @Rob-J-BJJ
    @Rob-J-BJJ Před 10 měsíci

    good stuff buddy

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 Před měsícem

    understood , thanks

  • @heyOrca2711
    @heyOrca2711 Před 4 měsíci

    Understood Sir!

  • @rahul-sinha
    @rahul-sinha Před 2 lety +17

    Man, your DP playlist on CZcams will gonna rock Man....!!!!! 🥳

  • @YourCodeVerse
    @YourCodeVerse Před 9 měsíci

    Understood✅🔥🔥

  • @srinathv1412
    @srinathv1412 Před 6 měsíci

    Mast understood !!!!!!

  • @msannitya6144
    @msannitya6144 Před 2 měsíci

    Ur really a saver of my life bhayya

  • @rushyya
    @rushyya Před 9 měsíci

    UNDERSTOOD!

  • @prabhakaran5542
    @prabhakaran5542 Před 3 měsíci +1

    Understood ❤

  • @AnandKumar-og8bu
    @AnandKumar-og8bu Před rokem +19

    Bhai main DSA wala banda hun nhi, I'm a pure developer and I hate DSA due to its toughness but randomly today I thought of watching a video of Striver and I'm amazed. I'm able to understand it very well.
    Hats off to you Striver for your explanation. Live a quality life bro ❤

    • @yashrajdeshmukh6759
      @yashrajdeshmukh6759 Před 9 měsíci +2

      I want to know how did you become a pure developer without DSA?

    • @iamnoob7593
      @iamnoob7593 Před 6 měsíci

      @@yashrajdeshmukh6759 U just need to know basics of DSA , OOPs Thats it .

  • @bhupendrasinghshahi5933
    @bhupendrasinghshahi5933 Před 2 lety +4

    understood. Please make a playlist for Greedy too. Thank You.

  • @shobhitsrivastava1223
    @shobhitsrivastava1223 Před 10 měsíci +1

    Understood 💗

  • @rutikabhuimbar4734
    @rutikabhuimbar4734 Před 2 lety +3

    Thank you so much for making such amazing content🙌❤

  • @user-ik3qu5uy5e
    @user-ik3qu5uy5e Před 5 měsíci

    understood sir !
    '

  • @vigneshkumar4990
    @vigneshkumar4990 Před 2 měsíci

    understood💙

  • @Hipfire786
    @Hipfire786 Před 3 měsíci

    understood everything

  • @HARSHA_27
    @HARSHA_27 Před 8 měsíci

    Understood!!🙇‍♂

  • @khalasianiket816
    @khalasianiket816 Před měsícem

    understood ❤

  • @rohanmadiratta6421
    @rohanmadiratta6421 Před 9 měsíci +1

    On the A2Z course u have this ques under arrays so how are we supposed to do it without dp?

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 4 měsíci

    Awesome Sir..................

  • @akankshaawasthi3932
    @akankshaawasthi3932 Před rokem +2

    In first iteration cost will be -6 so it will update it in cost or not ?
    Then in max profit it will update it by -6 or not?
    Or in mini while going i=2 how it will update min?

  • @iEntertainmentFunShorts
    @iEntertainmentFunShorts Před rokem +10

    This was the one of the toughest question on dp string we came across specially with the base case, and omitting the for loop in Reccurence equation.
    Thank you so much Striver Bhaiya you have made DP so intuitive like how to think from scratch
    Thanks you so much again 💗

  • @drishtirai864
    @drishtirai864 Před 4 měsíci

    Understood !! :)

  • @saurabhrthakur
    @saurabhrthakur Před 3 měsíci

    Understood!

  • @resetengineering
    @resetengineering Před rokem +1

    Wondering, if you should include this in DP playlist

  • @sahilsaini4574
    @sahilsaini4574 Před 3 měsíci +1

    how would be able to solve it using recurrence. I'm unable to figure it out

  • @AbjSir
    @AbjSir Před 9 měsíci

    understood

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před 2 měsíci

    Understood !!

  • @linhinNTU
    @linhinNTU Před rokem

    can anyone explain for me why the space complexity is O(1) like he said?

  • @supertron6039
    @supertron6039 Před 4 měsíci

    Understood.

  • @RogithRog
    @RogithRog Před 2 měsíci +1

    In java, Easy to understand

    public static void main(String[] args) {
    int arr [] = {7,1,5,3,6,4};
    int temp =0;
    int max = Integer.MIN_VALUE;
    for(int i=0;i

  • @rishav144
    @rishav144 Před 2 lety +17

    best DP series ever....thanks Striver 💛

  • @SmartStudyHub27
    @SmartStudyHub27 Před 9 měsíci

    I have a doubt. If there is an array of unknown values then how to find out the min value?

  • @user-cu7lp5bl9f
    @user-cu7lp5bl9f Před 11 měsíci +5

    Striver, you had been mentioning space optimization since a lot of videos on this playlist. But isnt it that it takes some amount of time to store the previous variables/vectors for previous rows into a temp vector? This would add up to the computation time right??? And in this era we care more of time reduction than space reduction. Kindly let me know if I am wrong, and if not, then why do we need space optimization? Thank you for your valuable time.

    • @AnshKumar-tj8bn
      @AnshKumar-tj8bn Před měsícem

      When using things on cloud, cost is charged effectively on both storage and number of operations/compute power used. Thats why maybe on a larger picture it's effective.

    • @jaishriharivishnu
      @jaishriharivishnu Před 18 dny

      some of the questions can't be solved without space optimization.
      let suppose you have n=100 and X=1e6... now if you make int dp[100][1e6] this will cost you 1e8 space... which is not possibe and will give you segmentation error... With "prev" and "cur" space optimization you can do it in 2*X space... i.e order of 6

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx Před 7 měsíci

    Understood

  • @shashankdaksh7554
    @shashankdaksh7554 Před 2 lety

    Can u please provide solution for tiling with dominoes. I have searched whole CZcams and web but there is no good explanation

  • @jaykumargupta7307
    @jaykumargupta7307 Před 2 lety

    question link given in the desc. box is wrong

  • @dreamyme543
    @dreamyme543 Před 7 měsíci

    Understood:)

  • @itsme9877
    @itsme9877 Před 5 měsíci

    Hello.. Why is it price[i] - mini?????

  • @armaanhadiq3741
    @armaanhadiq3741 Před rokem +2

    By the way Engineering means optimisation
    so we have to optimise things

  • @JatinGupta-ze6nc
    @JatinGupta-ze6nc Před 5 měsíci

    Bhaiya I m starting today DSA sheet I will mark in comments which day I m seeing your video like day 5 video I will comment in your video day 5completed it help me to make consistent thanks bhaiya

  • @reddevilbeast15
    @reddevilbeast15 Před rokem +1

    Sir can you please cover Linked List like Array series.Within a month ??

  • @imtsrk04
    @imtsrk04 Před 8 měsíci +2

    4th November 2023, I'm watching this video while seeing Arrays Topic. Commenting here to see how long it takes for me to reach the same video in DP Playlist.

  • @sumdeb1987
    @sumdeb1987 Před rokem

    what is the white board drawing app you are using?

  • @aryanmaniyar7301
    @aryanmaniyar7301 Před 11 měsíci +2

    Amazing explaination :)

  • @niladrisekharnath
    @niladrisekharnath Před 2 lety

    How is Dynamic Programming ?

  • @itzzz48
    @itzzz48 Před 8 měsíci +1

    Why the lecturers never teach like this during college🙁

  • @md.imrankhan6912
    @md.imrankhan6912 Před 10 měsíci

    magician

  • @anirudhrana5578
    @anirudhrana5578 Před 16 dny

    🔥🔥🔥🔥🔥🔥

  • @gentleman7060
    @gentleman7060 Před rokem

    Why u didn’t do it with recursive dp?

  • @prateek4279
    @prateek4279 Před 2 měsíci

    badiya

  • @rrgofficial9481
    @rrgofficial9481 Před měsícem

    Understood boss\/

  • @rishabhgupta1222
    @rishabhgupta1222 Před 3 měsíci

    Understood DP Striver Sir

  • @saketpatel8155
    @saketpatel8155 Před 2 lety +5

    I was waiting for "whenever your heart is broken" and it never came XD

  • @suyashshinde2971
    @suyashshinde2971 Před rokem +1

    SDE Sheet Day 1 Problem 6 Done!

  • @ajitpal0821
    @ajitpal0821 Před 2 lety

    We also apply that first find min from array then from that index to end find the maximum element,
    please reply @take U Forward

  • @kartikeymishra5647
    @kartikeymishra5647 Před 2 lety +15

    never knew that we called this also as dynamic programming 😅

  • @DevashishJose
    @DevashishJose Před rokem

    Understood. Thank you so much

  • @nishantgarg2815
    @nishantgarg2815 Před 2 lety +1

    was waiting for this one......

  • @thefourhourtalk
    @thefourhourtalk Před měsícem +1

    Can anyone please reply Will the sorting technique work here Will sort the entire array And by difference out between the starting and the last index and of course, the difference would be maximum and will return the difference???????????/

    • @tbcreations2033
      @tbcreations2033 Před měsícem +1

      No, it will not work.As it will destroy the sequence.

    • @thefourhourtalk
      @thefourhourtalk Před měsícem

      @@tbcreations2033 okay sir thanks so much for replying

  • @jaishriharivishnu
    @jaishriharivishnu Před 18 dny

    4:00 i will approach this with priority queue

  • @iamnoob7593
    @iamnoob7593 Před 6 měsíci

    US striver

  • @kunalbandooni4009
    @kunalbandooni4009 Před 2 lety

    Sir, there was no song in the end of this video :(

  • @AnkitSingh-wq2rk
    @AnkitSingh-wq2rk Před 2 lety

    Bhai i am also a working professional but I wanted to ask itna sab daily kaise karte ho ? office ka kaam phir video recording ya live stream ? burnout nahi feel karte kya ? I am on kinda similar grind of upskilling ... giving contest learning new tech stacks but kabhi kabhi social life ki L lag jate hai ...

  • @chetanthakral5322
    @chetanthakral5322 Před 2 lety

    The problem link in description is leading to some other problem.

  • @nagame859
    @nagame859 Před rokem

    Damn well understood!!

  • @ayushyadav8851
    @ayushyadav8851 Před 2 lety +1

    When doing the problem without knowing that it's dp feels so easy.

  • @googleit2490
    @googleit2490 Před rokem

    Understood :)
    Done myself earlier itself
    Aug'1,2023 10:10am

  • @109_debjitacharjee9
    @109_debjitacharjee9 Před 8 měsíci

    ❤❤

  • @kirtiverma4924
    @kirtiverma4924 Před 11 měsíci

    man you are amazing
    how are you able to think like this

  • @SaifAliKhan-nm4em
    @SaifAliKhan-nm4em Před 2 lety +5

    I did this same question with dp without knowing I was doing dp 😅.

    • @siyangaming3242
      @siyangaming3242 Před 2 lety

      same with me bro . with same time complexity O(N) and O(1)

  • @v.sreesairam9488
    @v.sreesairam9488 Před 2 lety

    bhaiya please make a set of questions so that we can easily solve leetcode contest questions

  • @asikakhatoon3091
    @asikakhatoon3091 Před rokem

    Time loss.... How we'll be assume which day it'll be 1 and which day it'll be 6

  • @suyashajain1952
    @suyashajain1952 Před 9 měsíci

    21

  • @bhaiookabhai
    @bhaiookabhai Před 6 měsíci

    this code is not accepting anymore

  • @shivanibisht8111
    @shivanibisht8111 Před 4 měsíci

    Can anybody tell me why we have to keep track of minimum cost too

  • @LowkeyCoder
    @LowkeyCoder Před 7 měsíci

    "understood".

  • @RohitKumar-dy2gc
    @RohitKumar-dy2gc Před 8 měsíci

    US

  • @oqant0424
    @oqant0424 Před rokem

    understood.!!!!!!!!!!!!!!!

  • @AshokKumar-ii4qr
    @AshokKumar-ii4qr Před rokem

    How to do with memoization?

  • @akashsahu2571
    @akashsahu2571 Před rokem

    yes

  • @gsampath8017
    @gsampath8017 Před 2 lety +1

    Thanks striver ❣️

  • @pratikkumarsingh8114
    @pratikkumarsingh8114 Před rokem

    Thank you, bhaiya!!

  • @tvrao123
    @tvrao123 Před 4 měsíci

    this solution is wrong for the test case, price = [100,180,260,310,40,535,695]