Pattern
Pattern
Pattern
Use last-in-first-out state for matching, monotonic order, or deferred work.
Push unresolved items; pop when the current item resolves the top.
stack = []
answer = [-1] * len(nums)
for i, value in enumerate(nums):
while stack and nums[stack[-1]] < value:
j = stack.pop()
answer[j] = value
stack.append(i)