Next Greater Element II LeetCode solution in Python
1class Solution:2def nextGreaterElements(self, nums: List[int]) -> List[int]:3st = []4n = len(nums)5ans = [-1] * n6for i in range(2 * n - 1, -1, -1):7while st and st[-1] <= nums[i % n]:8st.pop()9if st and i < n:10ans[i] = st[-1]11st.append(nums[i % n])12return ans