🔥[Python 3] Monotonic stack 2 traversal approaches
Iteration from start to end:\npython3\nclass Solution:\n    def nextGreaterElements(self, nums: List[int]) -> List[int]:\n        stack, L = [], len(nums)\n    
## Iteration from start to end:\n```python3\nclass Solution:\n    def nextGreaterElements(self, nums: List[int]) -> List[int]:\n        stack, L = [], len(nums)\n        res = [-1] * L\n\n        for i in range(L*2):\n            idx = i % L\n            while stack and nums[stack[-1]] < nums[idx]:\n                res[stack.pop()] = nums[idx]\n            stack.append(idx)\n        return res\n```\n## Iteration from end to start:\n```\nclass Solution:\n    def nextGreaterElements(self, nums: List[int]) -> List[int]:\n        stack, L = [], len(nums)\n        res = [-1] * L\n\n        for i in reversed(range(L * 2 - 1)):\n            idx = i % L\n            while stack and stack[-1] <= nums[idx]:\n                stack.pop()\n            res[idx] = stack[-1] if stack else -1\n            stack.append(nums[idx])\n        \n        return res\n```\n\n
['Monotonic Stack', 'Python', 'Python3']