Daily Temperatures - Monotonic Stack - Leetcode 739 - Python

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

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

  • @shaisundar7116
    @shaisundar7116 2 года назад +276

    A small optimization - you need not store the (key, index) pair. You can just store the index in the queue. For each comparison just reference the array with the index in the queue. Got a faster compute and lesser space.

    • @asmahamdym
      @asmahamdym Год назад +6

      Great tip
      Thanks

    • @festraider
      @festraider Год назад +6

      Kyu(queue)

    • @randomystick
      @randomystick Год назад +31

      for anyone wondering how the code will look like with this modification (imagine a "sliding window" technique of comparing index l and index r):
      answer = [0] * len(temperatures)
      stack = []
      for r in range(len(temperatures)):
      while stack and temperatures[stack[-1]] < temperatures[r]:
      l = stack.pop()
      answer[l] = r-l
      stack.append(r)
      return answer

    • @trevor6711
      @trevor6711 4 месяца назад

      @@randomystick can you explain what he did with stackInd? it makes no sense to me. how is he indexing in his list with a variable that has no value but was initialized?

    • @_carrbgamingjr
      @_carrbgamingjr 3 месяца назад +1

      @@trevor6711 since he used an array to store temp/index, he was able to extract the index, which is array[1], from the temp, which is array[0].
      If he did X = stack.pop(), X would be equal to [array[0], array[1]]
      Since he did X, Y = stack.pop(), X = array[0] and Y = array[1]

  • @nguyen-dev
    @nguyen-dev 2 года назад +108

    The first time hearing monotonic stack. The solution provided in leetcode with space complexity O(1) is even more amazing!!! This problem to me is too hard. I was only able to find brute force solution before watching this video and see leetcode solutions tab.

    • @justsvk1500
      @justsvk1500 2 года назад +77

      dw man.. the more you don't know and the more you look it up when you don't know, the more patterns you'll recognise later

    • @anangelsdiaries
      @anangelsdiaries 5 месяцев назад +2

      I followed the advice of find a brute force solution first and then see if you can improve it. The brute force approach was relatively easy to find, but then... blank. I figured it'd had something to do with a stack, but dang.

  • @elitea6070
    @elitea6070 6 месяцев назад +68

    Man this question is annoying its basically "if you know this you know it" aint no way I was gonna do it without this video

    • @digitulized459
      @digitulized459 5 месяцев назад +2

      I was able to come up with the sub-optimal min-heap solution, but wouldn't have come up with this.

    • @noz9117
      @noz9117 4 месяца назад +15

      which is a lot of leetcode questions

    • @omarllama
      @omarllama 3 месяца назад

      Got the solution quickly, took me an hour to implement. beats 30% runtime and 80% memory. But remember when solving this problem, we know in advance it has to do with Stacks, so use that as a hint.

    • @curious_joe8560
      @curious_joe8560 2 месяца назад

      @@omarllama that's mediocre , you're mediocre .

    • @Socsob
      @Socsob Месяц назад +1

      I was able to solve this one pretty quickly as I had done the NeetCode Minimum Stack question before which had stumped me. The code is pretty different but it felt like the same concept of tracking mins/max in one direction

  • @swapneelchakraborty3553
    @swapneelchakraborty3553 Год назад +13

    Got TLE exception with the n^2 solution, thanks for this approach

    • @semakozosu2053
      @semakozosu2053 2 месяца назад

      Same here but optimized to 0(100n), worked but wasn't optimal

  • @Nekaelia
    @Nekaelia Год назад +25

    I found this one really hard, i was only able to find a brute force solution. Thank you for the clean explanation. ^_^

  • @MrjavoiThe
    @MrjavoiThe Год назад +15

    This is so hard not beginner at all, there must be way easier problems covering the basics and more beginner friendly than this, no way someone solved this way, without solving similar problems like this before.

  • @ceciljoel9577
    @ceciljoel9577 10 дней назад +1

    Using two pointers you can get O(1) space complexity

  • @nazninmalek4440
    @nazninmalek4440 2 года назад +22

    Another way is to traverse from right to left in an array and start building a stack, that way for every position we should be able to find next warmer temperature on top of the stack

    • @anantprakashsingh8777
      @anantprakashsingh8777 2 года назад +1

      Can you please share the code for this approach, I was trying to build a solution in a similar manner but couldn't really come to a conclusion. Thanks!

    • @krushnapatil9196
      @krushnapatil9196 2 года назад +2

      @@anantprakashsingh8777 leetcode pr discussion mein hai

    • @abhinavchoukse2357
      @abhinavchoukse2357 6 месяцев назад

      @@anantprakashsingh8777 I too tried this approach and here's how my solution looks like:
      class Solution {
      public:
      vector dailyTemperatures(vector& temperatures) {
      int n = temperatures.size();
      vector res(n, 0);
      stack st;
      st.push(make_pair(temperatures[n-1], n-1));
      for(int i=n-2; i>=0;i--){

      while(!st.empty() && st.top().first

    • @richardliu8747
      @richardliu8747 2 месяца назад

      @@anantprakashsingh8777
      public int[] dailyTemperatures(int[] temperatures) {
      int n = temperatures.length;
      int[] res = new int[n];
      Deque stack = new ArrayDeque();
      for (int i = n - 1; i >= 0; i--) {
      while (!stack.isEmpty() && temperatures[i] >= temperatures[stack.peek()]) {
      stack.pop();
      }
      if (stack.isEmpty()) {
      res[i] = 0;
      } else {
      res[i] = stack.peek() - i;
      }
      stack.push(i);
      }
      return res;
      }

    • @rafeeali8307
      @rafeeali8307 2 месяца назад

      i like this way more its like we know what future element will be before processing past ones

  • @EmilyHopeGrove
    @EmilyHopeGrove 8 месяцев назад +13

    I THINK this is a mis-speak here at 10:17, you say "first thing we're going to do is see is our stack empty, and if it is ... " I believe you meant to say "if it's NOT empty, and if it's not ..." (meaning there is indeed something inside the stack).
    To be clear -
    ```while stack and t > stack[-1][0]:``` - This while loop runs as long as stack is not empty and the current temperature t is greater than the temperature at the top of the stack (stack[-1][0]).
    Hope this helps someone who was just as confused as I was about why we would be able to get the temperature and index out of an empty stack 😂
    (This is not to criticize the world's greatest human on the planet for helping us lowly juniors get a handle on leetcode, it's just to help clarify for anyone who may have been confused).

    • @joydeep_
      @joydeep_ 5 месяцев назад

      Right!

    • @vickysekhon4866
      @vickysekhon4866 4 месяца назад

      noticed this too in his explanation, threw me off as well

  • @jessw4195
    @jessw4195 10 месяцев назад +7

    This was the first problem following Neetcode Roadmap that I was able to do by myself since I started studying two weeks back. Thanks for these videos!

  • @vaishnavejp9247
    @vaishnavejp9247 Год назад +9

    I am currently doing you roadmap. My god, thank you so so so much for that. I am so grateful for that. It truly has helped grinding leetcode fun.

  • @Sulerhy
    @Sulerhy 8 месяцев назад +2

    I think there are not many people who can solve this problem in the first time without knowing about Monotonic Stack. Too hard.
    But this algorithm is so cool, worth to learn

    • @elitea6070
      @elitea6070 6 месяцев назад +1

      I agree bro had no clue how to do it - at first I tried brute force but when I saw this video I was like "no way I was gonna think of this"

  • @CyberMew
    @CyberMew 3 года назад +10

    We can also get rid of the tuple and just store the indices (without the values) in the stack, since it's already exist in the temperatures array and we can use it...

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

      Totally makes sense .

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

      Assuming that I understand this correctly, I think that this might not work with duplicate temperatures (finding the index of a temperature value will only return its first occurence).

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

      @@thermalmedia6454 No fetching from array using the index you can get the exact element.

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

      👏 Yeap, just what I was thinking. Then use the original array to get the temp at that index

    • @begenchorazgeldiyev5298
      @begenchorazgeldiyev5298 2 года назад +1

      @@thermalmedia6454 No, we will get the temperature at the index we initially encountered it. Supposed you added index of 1 to the stack, when we get it from the stack either via peek() or pop(), and retrieve it from initial array temperatures[1], it will give us the temperature at that exact index.

  • @quirkyquester
    @quirkyquester Месяц назад

    if you wonder if its O(n) or O(n^2) time, read this: its still O(n) even in worst scenario if we have [4,3,2,1], our stack would be [4,3,2,1], however, when we at 3, we know 3 < 4, so we don't have to loop through the whole stack, we simply append it, thats O(1), same for all the other operations, we simply return the res with everything as 0, even if we have something like [4,3,2,1,5], we only process [4,3,2,1] once when we reach 5 (bigger than all of them). thats only O(n), so maximumly, we process each node twice and thats it, imagine we have [4,3,2,1,3], when we reach the last 3, we process only the [2,1,] from [4,3,2,1], then we stop, our stack would look like [4,3], the algorithm is done

  • @belindaqi243
    @belindaqi243 2 года назад +2

    Your coding tutorial helped me so much. Thank you.

  • @narendrakumariitb
    @narendrakumariitb 2 года назад +10

    Thanks for neet explanation 😊 Also I think we can solve it iterating from end as well

    • @n.h.son1902
      @n.h.son1902 8 месяцев назад

      How did you solve it by iterating from the end?

  • @themobilegamerzone4987
    @themobilegamerzone4987 5 месяцев назад

    One other possible solution instead of storing the temperature, index pair in the stack is to just store the indices directly and then do the comparisons in the while loop through indexing

  • @kobeissi721
    @kobeissi721 2 года назад +6

    Thanks! I did solve it but my mind jumped straight to DP (Like LIS) but it was pretty inefficient.

    • @samarthtandale9121
      @samarthtandale9121 Год назад +3

      Same bro ... In fact I have seen a pattern that the problems involving monotonic stack or monotonic queue often make us think it's about dp !

    • @EduarteBDO
      @EduarteBDO 10 месяцев назад

      Same. My head was stuck in dp until I saw the topics then I oh it's a stack problem. I mean there's some kind of memoization with monotonic stack

  • @n.h.son1902
    @n.h.son1902 8 месяцев назад +2

    Can someone explain to me why it's O(n) time complexity. Because we have one loop to iterate through all elements in the temperatures, and another loop on the stack? So my interpretation would be O(n^2) time in the worst case scenario. For example, if we have temperatures = [50, 49, 48, 47, 46, 45], then the stack = [[50, 0], [49, 1], [48, 2], [47, 3], [46, 4], [45, 5]]

    • @quirkyquester
      @quirkyquester Месяц назад

      actually its still O(n) even in worst scenario if we have [4,3,2,1], our stack would be [4,3,2,1], however, when we at 3, we know 3 < 4, so we don't have to loop through the whole stack, we simply append it, thats O(1), same for all the other operations, we simply return the res with everything as 0, even if we have something like [4,3,2,1,5], we only process [4,3,2,1] once when we reach 5 (bigger than all of them). thats only O(n), so maximumly, we process each node twice and thats it, imagine we have [4,3,2,1,3], when we reach the last 3, we process only the [2,1,] from [4,3,2,1], then we stop, our stack would look like [4,3], the algorithm is done

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

    there is a O(1) space soln there on leetcode

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

    Clean explaination!

  • @m11dedhia
    @m11dedhia 24 дня назад

    How is the space complexity of the DP approach constant? Shouldn't it be linear too since we are using a vector to return the final result. The only optimization is on the space we save by eliminating the use of a stack. So stack solution is O(2n) (which is basically O(n)) and DP solution is O(n).

  • @MrWuggles
    @MrWuggles Год назад +2

    Great solution explanation, although wouldn't this be more of an array problem? As I understand it you can only interact with the top of a stack, not the elements below it.

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

    best explainer of leetcode problems on the internet

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

    Love your videos! Thanks for the efforts!

  • @sidazhong2019
    @sidazhong2019 5 месяцев назад

    This question is more like a sliding window problem. However, the left pointer is not a "single" pointer, but a group of left pointers. After the sliding window condition is triggered, all left pointers are compared with the right pointer. it's genius.
    while [left1, left2, left3, left4] < right:
    instead of
    while left < right:

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

    How do I get from Brute force approach to this (or such) optimized solution?

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

    This is extremely useful even though I code in c++.

    • @TheElementFive
      @TheElementFive 3 года назад +50

      Cpp coders want everyone to know they code in cpp. They’re like vegans 😅

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

    Made so clear! Thank you.

  • @MP-ny3ep
    @MP-ny3ep 10 месяцев назад

    Great explanation as always. Thank you .

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

    Good job! and well done. I think the time complexity is still quadratic. Consider this example temperatures = [90, 80, 70, 60, 50, 40, 30, 100]

    • @BhanuPrakash-if2wl
      @BhanuPrakash-if2wl 2 года назад

      it is not quadratic bcoz in your example we are traversing 2N times not N*N times(i too got confused about this)

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

      Thanks@@BhanuPrakash-if2wl As long as there is an inner while loop inside an outer for loop, the worst. case time complexity is quadratic. It will only be 2 * N, if you exited the first for loop, before starting the second while loop.

    • @NeetCode
      @NeetCode  2 года назад +40

      Having an inner loop does not mean quadratic, that's a common mistake people make. You have to look at the context, the inner loop will never run more than N times total, so it can't be quadratic.

    • @shreshtashetty2985
      @shreshtashetty2985 2 года назад +1

      @@NeetCode Then what exactly is the time and space complexity?

    • @harishsn4866
      @harishsn4866 2 года назад +8

      @@shreshtashetty2985
      Time complexity is O(N) because each element can only be added to the stack once, which means the stack is limited to N pops. Every iteration of the while loop uses 1 pop, which means the while loop will not iterate more than N times in total, across all iterations of the for loop.
      This gives a time complexity of O(2N) =O(N).

  • @NikhilAsawadekar
    @NikhilAsawadekar 7 месяцев назад

    Better approach:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    """
    Always try to find the next greater so easier to start from right hand side
    """
    stack = []
    n = len(temperatures)
    result = [0] * n
    for i in range(n-1, -1, -1):
    #while the existing temperature is greater keep popping until you find the greater temperature in top of the stack
    while stack and temperatures[stack[-1]]

  • @KermitDominicano
    @KermitDominicano Месяц назад

    I was stumped on this one, and the solution is always so painfully obvious when I finally give up on a problem, lol

  • @JCT836
    @JCT836 8 месяцев назад

    Thanks! Great explanation!

  • @izzya8132
    @izzya8132 2 года назад +8

    I came up with this same O(n) solution, submitted it to Leetcode and got a Time Limit Exceeded error.
    Looked at my code for ten minutes, trying to figure out how I could possibly optimize it. Then I tried running the long input that broke my code, and it ran pretty fast so I thought "something is weird here", and just submitted the answer again. Got "faster than 86%"... Turns out my answer was fine the whole time, it was just lag or something on the first submission.
    I do appreciate Leetcode's problem set and how relevant it is to real interviews, but god I just hate how terribly inaccurate its speed testing is.

    • @MinhNguyen-lz1pg
      @MinhNguyen-lz1pg 2 года назад +2

      same here, I was slightly confused

    • @charlesdarkwind
      @charlesdarkwind 2 года назад +1

      Same, I get time exceeded like 75% o the time with the same solution

    • @johnwick2018
      @johnwick2018 2 года назад +1

      same here

    • @GoodLuck-dv2zu
      @GoodLuck-dv2zu 2 года назад +1

      Same here, I was like what the fu*k??. How to optimize this when its already O(N)

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

    Wow 😲 ... Brilliant solution!

  • @beeramrahulreddy11
    @beeramrahulreddy11 3 года назад +7

    To me at first glance it looked like to find the next greater element on right for each element

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

      same here. I used two pointers in my first attempt

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

    Great Explanation as always
    Thank you

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

    Thanks man, liked and commented for support

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

    @NeetCode can you do 629. K Inverse Pairs Array? Im stuck for this question for the whole day? Please help me. 😭

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

      its a fucking hard question i just left it.

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

      @@CEOofTheHood @NeetCode help us

  • @AdityaKumar-ec5th
    @AdityaKumar-ec5th 9 месяцев назад

    thank you mr. neet

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

    great visual explanation!!

  • @omeletter
    @omeletter 7 месяцев назад

    What are like characteristics of a problem where monotonic stack is suitable?
    I haven’t solved a lot of problem using mono stack, and having hard time knowing when to use it…

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

    amazing explanation dude, thank you

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

    Hey guys, here is my solution.. easier to understand I would hope:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    res = [0] * len(temperatures)
    stack = []
    for i in range(len(temperatures)):
    while stack and temperatures[stack[-1]] < temperatures[i]:
    j = stack.pop()
    res[j] = i - j
    stack.append(i)
    return res

  • @_sf_editz1870
    @_sf_editz1870 10 месяцев назад

    here the java code : class Solution {
    public int[] dailyTemperatures(int[] temp) {
    ArrayDeque stack = new ArrayDeque();
    int ans[] = new int[temp.length];
    // we need a strictly decreasing queue
    for(int i=0; i

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

    Thankyou for helping us.

  • @riceball100
    @riceball100 2 года назад +1

    Where does the variable stackT get used in the example code at 11:46 inside of the while loop?

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

      It doesn't. There is no need to add a pair [temp, index] to the stack -- just index is enough.

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

    I cannot know how can this be O(n). For only increasing and only equal input temp it is O(n).
    However, when there are increasing and decreasing temps in the array it it not O(n).
    You can try putting a count variable inside both the loops and see.

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

    Beautiful videos.

  • @marcelsantee1809
    @marcelsantee1809 Год назад +5

    O(n)?
    Shouldn't we take in consideration the while loop? In worst case, might take long.

  • @sadeceaka
    @sadeceaka Месяц назад

    nice explanation

  • @MIDNightPT4
    @MIDNightPT4 10 месяцев назад

    10:17 I think you meant if our stack is not empty

  • @atomicbreath4360
    @atomicbreath4360 2 года назад +2

    In worst case scenario the size of the stack will be 10^5 as n can be at the much. One alternate ways is to create a temp array of size 101 (as temperature cannot be more then 100) and when ever u want to find look at temp array starting from temperature [i+1] to 101. Time complexity is 0(100*n) but space will always remain constant more precise 0(100)

  • @devanshsharma5159
    @devanshsharma5159 8 месяцев назад

    I actually solved this using the sliding window but ended up failing test cases because it was too slow :| Using a monotonic stack is way easier.

  • @haolintang
    @haolintang 10 месяцев назад

    class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    tempidx = {}
    for i, n in enumerate(temperatures):
    tempidx[n] = i
    stack = []
    res = [0] * len(temperatures)
    for i in range(len(temperatures)):
    while stack and stack[-1] < temperatures[i]:
    val = stack.pop()
    idx = tempidx[val]
    res[idx] = i - idx
    stack.append(temperatures[i])
    return res
    why this is wrong in some cases

    • @jst8922
      @jst8922 8 месяцев назад

      There is an issue with the way the index of the value is calculated when updating the result (res) array.
      The index variable should be updated to the current index i instead of the index of the popped value from the stack.
      Here is the corrected code:
      class Solution:
      def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
      tempidx = {}
      for i, n in enumerate(temperatures):
      tempidx[n] = i
      stack = []
      res = [0] * len(temperatures)
      for i in range(len(temperatures)):
      while stack and temperatures[stack[-1]] < temperatures[i]:
      idx = stack.pop()
      res[idx] = i - idx
      stack.append(i)
      return res

    • @jst8922
      @jst8922 8 месяцев назад

      ```python
      class Solution:
      def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
      tempidx = {}
      for i, n in enumerate(temperatures):
      tempidx[n] = i
      ```
      - The `Solution` class contains a method called `dailyTemperatures` that takes a list of temperatures as input and returns a list of integers.
      - The `tempidx` dictionary is initialized to store the index of each temperature in the `temperatures` list.
      - The `enumerate` function is used to iterate over the `temperatures` list, providing both the index `i` and the temperature `n` at each iteration.
      - The index `i` of each temperature `n` is stored in the `tempidx` dictionary with the temperature as the key.
      ```python
      stack = []
      res = [0] * len(temperatures)
      ```
      - An empty stack `stack` is initialized to keep track of the indices of temperatures.
      - The `res` list is initialized with zeros, having the same length as the `temperatures` list. It will store the number of days to wait for a warmer temperature.
      ```python
      for i in range(len(temperatures)):
      while stack and temperatures[stack[-1]] < temperatures[i]:
      idx = stack.pop()
      res[idx] = i - idx
      stack.append(i)
      ```
      - The code iterates over the indices of the `temperatures` list using a `for` loop.
      - Inside the loop, a `while` loop is used to check if the stack is not empty and the temperature at the top of the stack (index `stack[-1]`) is less than the current temperature `temperatures[i]`.
      - If the condition is true, it means a warmer temperature is found. The index `idx` at the top of the stack is popped, and the corresponding value in the `res` list at index `idx` is updated with the difference between the current index `i` and `idx`, representing the number of days to wait for a warmer temperature.
      - After the `while` loop, the current index `i` is appended to the stack.
      ```python
      return res
      ```
      - Finally, the method returns the `res` list, which contains the number of days to wait for a warmer temperature for each corresponding temperature in the input list.
      The code uses a stack to keep track of the indices of temperatures and efficiently finds the next warmer temperature for each temperature in the list. The time complexity of this solution is O(n), where n is the length of the `temperatures` list, as each temperature is pushed and popped from the stack at most once.

  • @chickwithpizza535
    @chickwithpizza535 15 дней назад

    The drawing might be misleading, you are putting the comparison value into the stack in your drawing where it is not yet in the stack while you are comparing them. You only put that comparison value into the stack after each time you have finished comparing everything which is after each while loop.

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

    Thank you!!!

  • @nguyentuan1990
    @nguyentuan1990 5 месяцев назад

    i just dont understand how you can pop in the middle of the stack, like 69 is not on top of the stack, how can you pop it off? isnt stack LIFO? so you have to pop 72 before you can pop 69 right?

  • @janardannn
    @janardannn 5 месяцев назад

    i am so dumb, forgot that pairs exist
    that was the light buld moment, stack st;
    😭😭😭😭😭😭😭😭😭😭😭😭

    • @RuslanZinovyev
      @RuslanZinovyev 5 месяцев назад

      it can be solved without pairs

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

    Brilliant AF !!!!!!!!

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

    whats the tool you use for drawing the solutions?

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

    Could you cover #2104 sum of subarray ranges? Thanks :)

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

    thank you sir

  • @illu1na
    @illu1na 23 дня назад

    Guys this video seems outdated, his websites solution page shows more optimised DP solution without extra space.

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

    Fantastic!

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

    Thanks

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

    hey what is the run time you didnt tell in the video, and i dont know how to calculate it here because it is not like realy we pass time the array....

    • @o8XOX8o
      @o8XOX8o 2 года назад +2

      Runtime is O(n) - you simply iterate over the list of 'n' temperatures. Each iteration you push an element onto a stack O(1), and do some number of pops from the stack. Since we push 'n' elements, even though each step we can pop a variable amount of elements, the total number of these pops can't exceed 'n', so on average it's a single pop per iteration.
      In the loop:
      Pop stack and assign - O(1) average
      Push stack - O(1)
      Loop: O(n)
      Overall: O(n)
      Memory is also O(n) since worst case we would have a stack containing 'n' elements. We would run into this case if our temperatures were continually decreasing. 10,9,8,7,6,5,4,3,2,1 at the end of the iterations, our stack would contain every element ('n' elements), and our array to return would be all 0 since the condition of increasing was never met.

  • @hrsight
    @hrsight 8 месяцев назад

    Good video.

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

    Another version of next greater element problem!!!

  • @satyamsinghal930
    @satyamsinghal930 7 месяцев назад

    instead of keeping holes in the output array, i created the monotonic stack traversing backwards. LOL. Stupid things stacks make you do

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

    i tried this with function...but it will run only for small number of arrays

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

    Nice 👍🏻

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

      Hey, how are you, I don’t know you though, but I like your efforts buddy 👍🏻😊

  • @oobleck147
    @oobleck147 2 года назад +1

    How is this not O(n^2) ?, there is still an inner while loop whose length is 'n'?

    • @minciNashu
      @minciNashu 2 года назад +1

      actually it's O(n * 2), so O(n)

  • @evlntnt1121
    @evlntnt1121 10 месяцев назад +2

    Isn't it close to n^2 complexity in the worst case?

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

    Took me a while to see it, but once we do it's easy - not a fan of these trick questions where it's easy only if we solved it before.
    To me the solution is a stack that tracks the smallest value. If we come across a larger value, we keep popping from the stack. This works because values that work for larger values will also work for the smallest values on the top of stack.

  • @iRYO400
    @iRYO400 Год назад +1

    There is the 47th test case in LeetCode - where N = 10^5 and temperatures = [99,99,99,99,99...N-1, 100]. With this approach it exceeds time limit

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

    understood

  • @yunijkarki9088
    @yunijkarki9088 5 месяцев назад

    Even after the concept, i found it hard to code. Phew.

  • @venkatalaxmimounikabatchu4591
    @venkatalaxmimounikabatchu4591 2 года назад +1

    here we are using while loop inside for loop, so how come the time time-complexity is linear?

  • @dima13693
    @dima13693 2 месяца назад

    Tbh the "it's intuitive to use the stack here" is not right. To me it felt like you jumped to it without reason. Seeing the solutino i can see that it works, but it's taking a while to get an intuitive understanding why it works.

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

    U a God

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

    Excuse me sir but 69 is always greater. Sorry, I can’t help myself.

  • @Beverage21
    @Beverage21 7 месяцев назад +2

    wow i used to think that you explain things nicely but damn this is confusing. no offence

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

    8:09 You talk about popping the stack but are still using the popped values to be present to indicate distance (for the number 75 in your example). Popping doesn't allow for that as it shortens arrays, so this explanation doesn't make much sense.
    Also popping occurs from the end, shifting occurs from the beginning, and you're referring to "popping" numbers with earlier index values (inserted earlier) with items inserted later at the end, which also doesn't make sense at face value (you're placing the number in your stack in your explanation and then discussing popping earlier values).
    **EDIT** This only makes sense when you get to the code section and realize you're storing the index in the value. It makes no sense (as I commented above) to a first-time observer watching your drawn explanation.

  • @sonysherpa3184
    @sonysherpa3184 7 месяцев назад

    incase anyone is having difficulty understanding this. I found this video easier to understand.
    ruclips.net/video/ekFs9Nb2RNQ/видео.html

  • @kokojolo
    @kokojolo 3 месяца назад

    😘

  • @DetectiveConan990v3
    @DetectiveConan990v3 29 дней назад

    honestly i do not understand this at all

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

    why you speak so fast

  • @Ryan-sd6os
    @Ryan-sd6os 2 года назад

    slow down mate