Daily Temperatures - Leetcode 739 - Stacks (Python)

  Рет қаралды 9,738

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 23
@GregHogg
@GregHogg 6 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@moonlight-td8ed
@moonlight-td8ed 6 ай бұрын
coming to dsa in python, you and neetcode are the BEST
@alexandermillar3763
@alexandermillar3763 11 ай бұрын
Clear and easily followed visual explination. Thank you!
@GregHogg
@GregHogg 11 ай бұрын
Thank you and you're very welcome!
@javeedmahammad2129
@javeedmahammad2129 5 ай бұрын
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
@sohamarora1837
@sohamarora1837 3 ай бұрын
This was easier to understand than Neetcode's explanation. Thanks!
@reginatoronto
@reginatoronto Ай бұрын
u explain more easily than other channels, although the theory is same, but your explanation was great
@akashgeorge5433
@akashgeorge5433 2 ай бұрын
you are an MVP wonderfully explained
@JoeTan-nq4fq
@JoeTan-nq4fq 3 ай бұрын
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)
@nithin2782
@nithin2782 7 ай бұрын
Excellent explanation. Thanks!! Very helpful
@mattphillips6623
@mattphillips6623 11 ай бұрын
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!
@GregHogg
@GregHogg 11 ай бұрын
Haha you're doing great! Thanks so much :)
@chambora1312
@chambora1312 9 ай бұрын
Did I hear a "duck and the lemonade stand" reference at 1:03 😂
@GregHogg
@GregHogg 9 ай бұрын
You most definitely did haha
@TTTT-sj3vz
@TTTT-sj3vz 4 ай бұрын
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 ?
@tejasjoshi9140
@tejasjoshi9140 3 ай бұрын
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
@pradeepbhat1363
@pradeepbhat1363 11 ай бұрын
Is there any way to identify it as a stack pattern ?
@GregHogg
@GregHogg 9 ай бұрын
This one is monotonic stack where increasing elements kinda start to line up, you might get used to that pattern
@SemesterAtSeaHopeful
@SemesterAtSeaHopeful 4 ай бұрын
How do you know its not a two pointer problem?
@KarthikKumaresan-k1o
@KarthikKumaresan-k1o 9 ай бұрын
Phenomenal! Thanks
@GregHogg
@GregHogg 9 ай бұрын
Glad you found it helpful, thanks so much!
@kickbuttowsk2i
@kickbuttowsk2i 19 күн бұрын
greg > neetcode, fight me
@Tigerwolf70
@Tigerwolf70 13 күн бұрын
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
Daily Temperatures - Monotonic Stack - Leetcode 739 - Python
11:52
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 66 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 14 М.
Koko Eating Bananas - Leetcode 875 - Binary Search (Python)
13:34
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 690 М.
Starting a Career in Data Science (10 Thing I Wish I Knew…)
10:42
Sundas Khalid
Рет қаралды 269 М.