Python solution with detailed explanation
Solution with discussion https://discuss.leetcode.com/topic/78268/python-solution-with-detailed-explanation\n\nNext Greater Element II https://leetcode.com/prob
**Solution with discussion** https://discuss.leetcode.com/topic/78268/python-solution-with-detailed-explanation\n\n**Next Greater Element II** https://leetcode.com/problems/next-greater-element-ii/\n\n**Algorithm**\n* **Stack for next greater element**: Suppose we have a decreasing sequence followed by a greater number. For example [5, 4, 3, 2, 1, 6] then the greater number 6 is the next greater element for all previous numbers in the sequence.\n*  **Handling duplicates in input**: Push the index and value tuple on the stack instead of just the value. This makes sure duplicated elements are correctly handled. Example:[100,1,11,1,120,111,123,1,-1,-100] - we need to have the right answer for both 1s.\n*  **Handling circular array**: Process it twice. Example: [5,4,3,2,1]. By processing it twice, you are essentially doing: [5,4,3,2,1]+[5,4,3,2,1]. Typical pattern to solve circular array problems is to extend the original array to twice length, 2nd half has the same element as first half. Then everything become simple.\nhttps://discuss.leetcode.com/topic/77881/typical-way-to-solve-circular-array-problems-java-solution\n\n```\nclass Solution(object):\n    def nextGreaterElements(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: List[int]\n        """\n        cache, st = {}, []\n        for idx,x in enumerate(nums):\n            while st and st[-1][1] < x:\n                a,b = st.pop()\n                cache[a] = x\n            st.append((idx,x))\n        for idx,x in enumerate(nums):\n            while st and st[-1][1] < x:\n                a,b = st.pop()\n                cache[a] = x\n            st.append((idx,x))\n        result = [-1]*len(nums)\n        for idx,x in enumerate(nums):\n            if idx in cache:\n                result[idx] = cache[idx]\n        return result        \n```
[]