496. Next Greater Element I

https://leetcode.com/problems/next-greater-element-i/

还是daily temp那个decreasing stack的做法

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        res = []
        d = dict()
        stack = []
        for num in nums2:
            while stack and stack[-1] < num:
                k = stack.pop()
                d[k] = num
            stack.append(num)
        
        for num in nums1:
            if num not in d:
                res.append(-1)
            else:
                res.append(d[num])
        return res
        

Last updated

Was this helpful?