What is Daily Temperatures
For each day, return how many days you must wait until a warmer temperature. If no warmer day exists, the answer is 0.
Approach
Visualization & algorithm
The finished picture is on screen when the page opens. Next and Play start from the first step. The controls sit under the picture and the pseudocode.
Complexity
| Piece | Cost | Why |
|---|---|---|
| Time | O(n) | Each day is pushed and popped once. |
| Space | O(n) | A falling forecast fills the stack. |
Implementation
def solve(self, temps):
n = len(temps)
ans = [0]*n
st = []
for i, t in enumerate(temps):
while st and temps[st[-1]] < t:
j = st.pop()
ans[j] = i - j
st.append(i)
return ans