class Solution:
def nextGreaterElements(self, nums) :
n = len(nums)
res = [-1]*n
stack = []
# ”假装“该数组长度翻倍,然后用取模映射到原数组
for i in range(n*2-1, -1, -1):
while stack and stack[-1] <= nums[i%n]:
stack.pop()
res[i%n] = stack[-1] if stack else -1
stack.append(nums[i%n])
return res