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

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 มี.ค. 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

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

  • @takeUforward
    @takeUforward  2 ปีที่แล้ว +116

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

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

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

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

      When will remaining DP problems will come ?

  • @avishkarpatil5871
    @avishkarpatil5871 4 หลายเดือนก่อน +15

    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 หลายเดือนก่อน +2

      true.. it greedy works best for this question

    • @saatvikmangal7994
      @saatvikmangal7994 5 วันที่ผ่านมา +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 3 วันที่ผ่านมา

      Yes, I agree with you

  • @TW-uk1xi
    @TW-uk1xi 2 ปีที่แล้ว +200

    Because of this guy, I love dynamic programming.

    • @rahulbhagat4023
      @rahulbhagat4023 2 ปีที่แล้ว +30

      Yes u will until u start solving different questions

    • @ShubhamVerma-hw4uj
      @ShubhamVerma-hw4uj 2 ปีที่แล้ว +2

      @@rahulbhagat4023 so these questions are not enough?

    • @priyanshumohanty5261
      @priyanshumohanty5261 ปีที่แล้ว +8

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

    • @nanda_8
      @nanda_8 ปีที่แล้ว +12

      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 ปีที่แล้ว +3

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

  • @RAKSHITHPGBBTCSBTechCSE
    @RAKSHITHPGBBTCSBTechCSE 8 หลายเดือนก่อน +4

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

  • @LowkeyCoder
    @LowkeyCoder 7 หลายเดือนก่อน +4

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

  • @stith_pragya
    @stith_pragya 6 หลายเดือนก่อน +2

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

  • @johncenakiwi
    @johncenakiwi 2 ปีที่แล้ว +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.

  • @arkaprabhasaha8203
    @arkaprabhasaha8203 หลายเดือนก่อน +1

    Following your entire DSA A2Z Course!

  • @samuelfrank1369
    @samuelfrank1369 8 หลายเดือนก่อน +1

    Understood. Thanks a lot. Please upload more videos Bhaiyaaa

  • @Hrushi_2000
    @Hrushi_2000 9 หลายเดือนก่อน +1

    Understood. Thankyou Sir

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

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

  • @UECAshutoshKumar
    @UECAshutoshKumar 11 วันที่ผ่านมา +1

    Thank You!
    Understood

  • @AnandKumar-og8bu
    @AnandKumar-og8bu ปีที่แล้ว +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 9 หลายเดือนก่อน +2

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

    • @iamnoob7593
      @iamnoob7593 6 หลายเดือนก่อน

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

  • @Rob-J-BJJ
    @Rob-J-BJJ 9 หลายเดือนก่อน

    good stuff buddy

  • @rocktatnine
    @rocktatnine 8 หลายเดือนก่อน +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.

  • @heyOrca2711
    @heyOrca2711 3 หลายเดือนก่อน

    Understood Sir!

  • @YourCodeVerse
    @YourCodeVerse 8 หลายเดือนก่อน

    Understood✅🔥🔥

  • @prabhakaran5542
    @prabhakaran5542 2 หลายเดือนก่อน +1

    Understood ❤

  • @rahul-sinha
    @rahul-sinha 2 ปีที่แล้ว +17

    Man, your DP playlist on TH-cam will gonna rock Man....!!!!! 🥳

  • @shobhitsrivastava1223
    @shobhitsrivastava1223 9 หลายเดือนก่อน +1

    Understood 💗

  • @rushyya
    @rushyya 9 หลายเดือนก่อน

    UNDERSTOOD!

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

    Thank you bro. Understood

  • @msannitya6144
    @msannitya6144 2 หลายเดือนก่อน

    Ur really a saver of my life bhayya

  • @23lifeisgod
    @23lifeisgod 8 วันที่ผ่านมา

    Understood! thanks !! Long live !!!

  • @srinathv1412
    @srinathv1412 5 หลายเดือนก่อน

    Mast understood !!!!!!

  • @HARSHA_27
    @HARSHA_27 8 หลายเดือนก่อน

    Understood!!🙇‍♂

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 3 หลายเดือนก่อน

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

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 29 วันที่ผ่านมา

    understood , thanks

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

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

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

  • @saurabhrthakur
    @saurabhrthakur 2 หลายเดือนก่อน

    Understood!

  • @drishtirai864
    @drishtirai864 3 หลายเดือนก่อน

    Understood !! :)

  • @user-ik3qu5uy5e
    @user-ik3qu5uy5e 4 หลายเดือนก่อน

    understood sir !
    '

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

    badiya

  • @Hipfire786
    @Hipfire786 2 หลายเดือนก่อน

    understood everything

  • @rutikabhuimbar4734
    @rutikabhuimbar4734 2 ปีที่แล้ว +3

    Thank you so much for making such amazing content🙌❤

  • @vigneshkumar4990
    @vigneshkumar4990 2 หลายเดือนก่อน

    understood💙

  • @supertron6039
    @supertron6039 4 หลายเดือนก่อน

    Understood.

  • @user-is6ky7pp2n
    @user-is6ky7pp2n หลายเดือนก่อน

    Understood !!

  • @rishav144
    @rishav144 2 ปีที่แล้ว +17

    best DP series ever....thanks Striver 💛

  • @khalasianiket816
    @khalasianiket816 19 วันที่ผ่านมา

    understood ❤

  • @user-cu7lp5bl9f
    @user-cu7lp5bl9f 10 หลายเดือนก่อน +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 20 วันที่ผ่านมา

      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.

  • @RogithRog
    @RogithRog หลายเดือนก่อน +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

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

    Wondering, if you should include this in DP playlist

  • @imtsrk04
    @imtsrk04 8 หลายเดือนก่อน +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.

    • @AyushVerma-wu3nn
      @AyushVerma-wu3nn 4 หลายเดือนก่อน

      are you there yet buddy?

  • @AbjSir
    @AbjSir 9 หลายเดือนก่อน

    understood

  • @dreamyme543
    @dreamyme543 6 หลายเดือนก่อน

    Understood:)

  • @hardikpatel352
    @hardikpatel352 3 หลายเดือนก่อน

    Understood

  • @saketpatel8155
    @saketpatel8155 2 ปีที่แล้ว +5

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

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

    Understood boss\/

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

  • @armaanhadiq3741
    @armaanhadiq3741 ปีที่แล้ว +2

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

  • @md.imrankhan6912
    @md.imrankhan6912 9 หลายเดือนก่อน

    magician

  • @kartikeymishra5647
    @kartikeymishra5647 2 ปีที่แล้ว +15

    never knew that we called this also as dynamic programming 😅

  • @rohanmadiratta6421
    @rohanmadiratta6421 8 หลายเดือนก่อน +1

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

  • @SmartStudyHub27
    @SmartStudyHub27 8 หลายเดือนก่อน

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

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

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

  • @itzzz48
    @itzzz48 7 หลายเดือนก่อน +1

    Why the lecturers never teach like this during college🙁

  • @sahilsaini4574
    @sahilsaini4574 2 หลายเดือนก่อน +1

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

  • @SaifAliKhan-nm4em
    @SaifAliKhan-nm4em 2 ปีที่แล้ว +5

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

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

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

  • @JatinGupta-ze6nc
    @JatinGupta-ze6nc 4 หลายเดือนก่อน

    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

  • @rishabhgupta1222
    @rishabhgupta1222 2 หลายเดือนก่อน

    Understood DP Striver Sir

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

    SDE Sheet Day 1 Problem 6 Done!

  • @109_debjitacharjee9
    @109_debjitacharjee9 8 หลายเดือนก่อน

    ❤❤

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

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

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

      Yeah even I got the same idea

    • @user-ie8sy7wo4m
      @user-ie8sy7wo4m 11 หลายเดือนก่อน

      fails for this case 2 7 1 2. answer is 5 (7-2).

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

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

  • @aryanmaniyar7301
    @aryanmaniyar7301 11 หลายเดือนก่อน +2

    Amazing explaination :)

  • @akashyadagouda896
    @akashyadagouda896 5 หลายเดือนก่อน

    My mind OHHHHH!

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

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

  • @v.sreesairam9488
    @v.sreesairam9488 2 ปีที่แล้ว

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

  • @thefourhourtalk
    @thefourhourtalk 23 วันที่ผ่านมา +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 22 วันที่ผ่านมา +1

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

    • @thefourhourtalk
      @thefourhourtalk 22 วันที่ผ่านมา

      @@tbcreations2033 okay sir thanks so much for replying

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

    question link given in the desc. box is wrong

  • @ayushyadav8851
    @ayushyadav8851 2 ปีที่แล้ว +1

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

  • @iamnoob7593
    @iamnoob7593 6 หลายเดือนก่อน

    US striver

  • @AnkitSingh-wq2rk
    @AnkitSingh-wq2rk 2 ปีที่แล้ว

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

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

    Understood. Thank you so much

  • @nishantgarg2815
    @nishantgarg2815 2 ปีที่แล้ว +1

    was waiting for this one......

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

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

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

      Let m get that corrected

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

    Damn well understood!!

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

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

  • @Rishabh-rp1mi
    @Rishabh-rp1mi 28 วันที่ผ่านมา

    US

  • @kirtiverma4924
    @kirtiverma4924 10 หลายเดือนก่อน

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

  • @itsme9877
    @itsme9877 5 หลายเดือนก่อน

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

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

    How is Dynamic Programming ?

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

    plzz make a video on maths required for competitive programming

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

    what is the white board drawing app you are using?

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

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

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

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

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

    Why u didn’t do it with recursive dp?

  • @LowkeyCoder
    @LowkeyCoder 7 หลายเดือนก่อน

    "understood".

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

    Part -1 Beautiful.

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

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

  • @gsampath8017
    @gsampath8017 2 ปีที่แล้ว +1

    Thanks striver ❣️

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

    brilliantly explained

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

    Understood !!!

  • @shivanibisht8111
    @shivanibisht8111 3 หลายเดือนก่อน

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

    • @user-fw4kz3bb4g
      @user-fw4kz3bb4g 3 หลายเดือนก่อน +1

      to buy it at the lowest cost

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

    Understood kaka!

  • @suyashajain1952
    @suyashajain1952 8 หลายเดือนก่อน

    21

  • @unknown_coder7960
    @unknown_coder7960 2 ปีที่แล้ว +1

    Ye shi tha guru!

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

    yes