Master Data Structures & Algorithms For FREE at AlgoMap.io!
@moonlight-td8ed6 ай бұрын
coming to dsa in python, you and neetcode are the BEST
@alexandermillar376311 ай бұрын
Clear and easily followed visual explination. Thank you!
@GregHogg11 ай бұрын
Thank you and you're very welcome!
@javeedmahammad21295 ай бұрын
Hey Greg, I think we can modify the code and reduce the complexity of the code. By storing just the indices in the stack, we can directly access the temperature values using these indices, which simplifies the code. This approach makes it more efficient in terms of memory usage, as we're only storing indices rather than tuples containing both temperatures and indices. Here is the code: class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: n = len(temperatures) stack = [] res = [0] * n for i in range(n): while stack and temperatures[i] > temperatures[stack[-1]]: index = stack.pop() res[index] = i - index stack.append(i) return res
@sohamarora18373 ай бұрын
This was easier to understand than Neetcode's explanation. Thanks!
@reginatorontoАй бұрын
u explain more easily than other channels, although the theory is same, but your explanation was great
@akashgeorge54332 ай бұрын
you are an MVP wonderfully explained
@JoeTan-nq4fq3 ай бұрын
Traversing backwards and define stack to store indices for i in range(n-1, -1, -1): while stack and temperatures[i] >= temperatures[stack[-1]]: stack.pop() if stack: answer[i] = stack[-1] - i # Store the index offset from ith element stack.append(i)
@nithin27827 ай бұрын
Excellent explanation. Thanks!! Very helpful
@mattphillips662311 ай бұрын
I tried to solve this before watching the video and am as embarassed as i am impressed with myself haha. I did end up with a solution for all cases but do i ever have a lot to learn. Great content!
@GregHogg11 ай бұрын
Haha you're doing great! Thanks so much :)
@chambora13129 ай бұрын
Did I hear a "duck and the lemonade stand" reference at 1:03 😂
@GregHogg9 ай бұрын
You most definitely did haha
@TTTT-sj3vz4 ай бұрын
I did it like this :def dailyTemperatures(self, temperatures: List[int]) -> List[int]: res = [0] * len(temperatures) for i in range(len(temperatures)): for j in range(i+1,len(temperatures)): if temperatures[j] > temperatures[i]: res[i] = j-i break return res but the problem was the time limit, couldn't have thought about the monotonic stack edit why you appended t,i instead i,t ?
@tejasjoshi91403 ай бұрын
this problem is exact replica of next greater element, its just recognizing patterns, and about the "why t,i and not i,t" you can do it either way
@pradeepbhat136311 ай бұрын
Is there any way to identify it as a stack pattern ?
@GregHogg9 ай бұрын
This one is monotonic stack where increasing elements kinda start to line up, you might get used to that pattern
@SemesterAtSeaHopeful4 ай бұрын
How do you know its not a two pointer problem?
@KarthikKumaresan-k1o9 ай бұрын
Phenomenal! Thanks
@GregHogg9 ай бұрын
Glad you found it helpful, thanks so much!
@kickbuttowsk2i19 күн бұрын
greg > neetcode, fight me
@Tigerwolf7013 күн бұрын
I don’t like the way you code, bro setting variables to parameters, why not just use the parameter. For example, temps = temperatures. Like bro just use temperatures. Your style of programming is annoying