You are given an array of integers temperatures where temperatures[i] represents the daily temperatures on the ith day.
Return an array result where result[i] is the number of days after the ith day before a warmer temperature appears on a future day. If there is no day in the future where a warmer temperature will appear for the ith day, set result[i] to 0 instead.
Example 1:
Input: temperatures = [30,38,30,36,35,40,28]
Output: [1,4,1,2,1,0,0] Example 2:
Input: temperatures = [22,21,20]
Output: [0,0,0]
- Loop through each temperature
- If current temperature is warmer than temperature at stack's top index: Calculate waiting days = current index - popped index
- Keep popping and calculating until stack is empty or find a warmer day
Add current index to stack https://www.youtube.com/watch?v=cTBiBSnjO3c
In [ ]:
def dailyTemp(temp):
res=[0]*len(temp)
stack = []
for r in range(len(temp)):
while stack and temp[r]>temp[stack[-1]]:
l=stack.pop()
res[l]=r-l
stack.append(r)
return res