单调栈的应用.
class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: size = len(nums) ret = [-1] * size stack = [] for i in list(range(size)) * 2: while stack and stack[-1][1] < nums[i]: ret[stack.pop()[0]] = nums[i] stack.append((i, nums[i])) return ret