Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StackEntry = namedtuple("StackEntry", "value index")
- class Solution:
- def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
- """ Solution: monotonically-increasing stack
- """
- stack = []
- output = []
- for i in range(len(temperatures)-1, -1, -1):
- if len(stack) == 0:
- output.append(0)
- stack.append(StackEntry(value=temperatures[i], index=i))
- else:
- current_temp = temperatures[i]
- while len(stack) > 0:
- if current_temp >= stack[-1].value:
- stack.pop()
- else:
- break
- if len(stack) > 0:
- output.append(stack[-1].index - i)
- else:
- output.append(0)
- stack.append(StackEntry(value=temperatures[i], index=i))
- return reversed(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement