Python 7 lines easy to understand solution with Stack


  • 0
    T
    def nextGreaterElements(self, nums):
        res = [-1] * len(nums)
        stack = []
            for i in range(len(nums)) * 2:
                while stack and (stack[-1][1] < nums[i]):
                    res[stack.pop()[0]] = nums[i]
                stack.append((i, nums[i]))
        return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.