Daily Temperatures - Leetcode 739 - Stacks (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

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

javeedmahammad
Автор

bless ur heart, u rly are a lifesaver sometimes when neetcode’s solution isnt clear

charafeddine
Автор

coming to dsa in python, you and neetcode are the BEST

moonlight-tded
Автор

This was easier to understand than Neetcode's explanation. Thanks!

sohamarora
Автор

Clear and easily followed visual explination. Thank you!

alexandermillar
Автор

u explain more easily than other channels, although the theory is same, but your explanation was great

reginatoronto
Автор

9:02 I'm sure you're aware of this but you could simply change the parameters' name to "temps".

Great explanation as always ✨

viridianite
Автор

Excellent explanation. Thanks!! Very helpful

nithin
Автор

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)

JoeTan-nqfq
Автор

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!

mattphillips
Автор

Did I hear a "duck and the lemonade stand" reference at 1:03 😂

chambora
Автор

Is there any way to identify it as a stack pattern ?

pradeepbhat
Автор

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 ?

TTTT-sjvz
Автор

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

Tigerwolf
welcome to shbcf.ru