739. Daily Temperatures | Monotonic Stack | Space Optimisation O(1)

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

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

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

    That space optimization part.....just mindblowing !!!
    Thank you for the video !!

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

    Never thought that wow!Real time use of memoization

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

    You're so underrated bro, I just loved your explanation. Thanks for your O(1) Space Optimization :)

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

    Maza a gaya yr. Question karo to aise, analyse karke. PhD in DS Algo.

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

    ooooooooooo bhaiiiiiiiiii never ever thought that this question can be solved in this way too
    what an explaination bhaiiii
    subscribed you after watching the optimisation part bro

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

    Great solution. Just one suggestion, It would be better if you can prove that there will be O(n) comparisons in total rather than giving a bunch of examples for it.
    Edit - Case where last solution doesn't work linearly -
    [x,1, x,1, x,1 ... 2,3,4,5...,x]
    For each [x,1] that appears on the left, there will be x iterations. Say x = n/2 . Then the number of comparisons in that case array will be n/4 * n/2.

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

    So nicely explained 👍👍

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

    Sir, it's truly ingenious, thanks for proving this beautiful solution. But while I don't claim to have any expertise in this area, I would like to express my understanding that, theoretically, the space complexity appears to be O(n) due to the proportional dependence on the input size. It's worth noting that the solution refers to those values in the array for calculations, effectively storing intermediate values within the array. Also from my limited knowledge, it seems that, for larger inputs, the solution might not be as memory-efficient as alternative approaches, such as using an additional stack and storing the result in the input array. If you could address these concerns, that would be greatly appreciated. Looking forward to your insights!

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

      @moupiya9286 But, bro here we are returning the vector as an answer, while computing the space complexity, we keep in mind that what is the extra space used for computing the answer or the required output, here the output required itself, is an array, this means this is what we are computing, and to compute this we need this, and TO NEED THIS, what extra space is used ?? Nothing, (but just a handful of variables which is O(1) in space complexity ), so we will not count this in our complexity, that is why the space complexity here is O(1), I hope I can make it understandable to you.

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

      @@SidharthjainSingla Thank you for the detailed explanation, and I now understand the reasoning behind the O(1) space complexity. It's certainly a beautiful solution. My curiosity extends to a broader comparison-when there are no constraints on modifying the input array, do you think the O(1) approach is generally more advantageous than an O(k) solution using a stack? Since the constraints didn't limit modifications to the input array, I'm interested in understanding the practicality of these different approaches.
      I must admit that I only watched the O(1) part of the video, not the entire video, so I'm unsure whether the input array was modified. However, my initial thought is that many people might have opted for modifying the input array since there were no constraints about that. Looking forward to hearing your insights and thoughts. Thank you once again.

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

      @@moupiya9286 In the first approach, we used the *Monotonic Stack*
      (when *Stack* is used with some constraints like push the number only when top of the stack is smaller or greater or same with pop i.e., when the current element is big or small than top of the stack ,i.e., when we are constraining the stack to just store numbers in a *increasing* or *equal and increasing* or *decreasing* or *equal and decreasing* , This property or fashion of using stack is known as monotonic stack) for storing the next greater element for the current element and doing this for all the elements present in the array.
      In this approach ofcourse the time complexity is O(n) where n is the number of elements in the given array.
      Why the time complexity is O(n) because in the worst case it might be possible that the given array is sorted in non decreasing order then we need to insert all the elements in the array.
      In the second approach, we didn’t alter the data present in the given array, but the answer array we created which will not be comprised in the space complexity because this we have to return it to the function and the thing we return to the function in *most cases* is not accountable in space complexity *unless* it is mentioned that you have to return the same given array with some altering done on it and if then you used a another array then it will be counted in the space complexity because it was mentioned that you have to return the same given array with some alterations done on it.
      In the second approach,
      we used the calculated values of the answer array to calculate the next greater element for the current element ,
      you can say it is some sort of a dp-memoization which we do in dp not exactly this we do , but yeah sort of, dp actually means to solve the sub-problems and to calculate another sub problem we used the results of the calculated sub-problems. This is the basic definition of dynamic programming.
      That is why the space complexity in second approach is O(1). Thanks buddy…:)

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

    bhai ko roz video banate time call aate hai 🤪

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

    great keep going 💪💪💪💪💪💪

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

    Bhaiya dsa playlist start kijiye phir se usme aise question kariye jisme sb topic ke pattern cover ho jaye

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

    hey I'm getting tle for this optimized approach ? is it because I used python ? But the stack solution worked🤔

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

      because its not O(n)

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

    if we have (10^4) ,1,2,3,4,5,6,.............(10^4)+1 in this case firstly from back we tend to iterate and calculate the warmer day till index 1 it will be (n-1) and at index 0 it will again iterate i.e: n-1 +(1+1+1+1+1+1+1+....n) which will be 2*n is it?

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

      but this is just one worst case and for sure this is O(2* n) and we know that O(k*n) is the same as O(n).

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

    Amazing>>>

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

    how can you say that the given question is medium hard it most easy problem ever encountered

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

    Wowwww

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

    But for this test case 8 1 2 3 4 5 6 7 9 it will be n2 right

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

      its 2*n as except 8 everyone is just being compared once

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

    How is this O(1) space when the vector ans that your are returning is of size n?

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

      bruhh

    • @Idk-qg7hb
      @Idk-qg7hb 7 หลายเดือนก่อน +5

      Jaa tu tcs ke liye bana h 😂😂😂

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

      You are confusing between time complexity and space complexity it seems. Pls read about them indepth & comeback.

    • @satyamraj2779
      @satyamraj2779 7 หลายเดือนก่อน +10

      wow, i guess people are rude nowadays, trust me bro, there is no silly doubts, and also know that only incompetent people will comment such type of things and will judge you, do not get demotivated, keep going

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

      Actually he has excluded the space required for the stack to find next greater value but returning vector is required for the question as question is asking for next greater value at each index

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

    Abea Late q hua !

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

    🫡