classSolution:defnextGreaterElements(self,nums: List[int]) -> List[int]: n =len(nums) res = [-1] * n stack = []for i inrange(2* n):# 走两轮 num = nums[i % n]while stack and nums[stack[-1]]< num: index = stack.pop() res[index]= num stack.append(i % n)# option: i < n时才appendreturn res