<reponame>ccqpein/Arithmetic-Exercises<gh_stars>1-10
class Solution(object):

    def nextGreaterElements(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        stack, res = [], [-1] * len(nums)
        for i in range(len(nums)):
            #print(stack, res)
            while stack and (nums[stack[-1]] < nums[i]):
                res[stack.pop()] = nums[i]
            stack.append(i)

        # run twice because we need list be circle
        for i in range(len(nums)):
            #print(stack, res)
            while stack and (nums[stack[-1]] < nums[i]):
                res[stack.pop()] = nums[i]
            stack.append(i)
        return res

a = Solution()
print(a.nextGreaterElements([1, 2, 1]))  # [2,-1,2]
print(a.nextGreaterElements([5, 3, 4, 6, 7]))  # [6,4,6,7,-1]

class Solution(object):

    def nextGreaterElements(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        stack, res = [], [-1] * len(nums)
        for i in range(len(nums)):
            #print(stack, res)
            while stack and (nums[stack[-1]] < nums[i]):
                res[stack.pop()] = nums[i]
            stack.append(i)

        # run twice because we need list be circle
        for i in range(len(nums)):
            #print(stack, res)
            while stack and (nums[stack[-1]] < nums[i]):
                res[stack.pop()] = nums[i]
            stack.append(i)
        return res

a = Solution()
print(a.nextGreaterElements([1, 2, 1]))  # [2,-1,2]
print(a.nextGreaterElements([5, 3, 4, 6, 7]))  # [6,4,6,7,-1]
:type nums: List[int]         :rtype: List[int] #print(stack, res) # run twice because we need list be circle #print(stack, res) # [2,-1,2] # [6,4,6,7,-1]