2025-07-29 17:45:22 +0000 UTC

Next Greater Element I

Code

class Solution:
    def nextGreaterElement(self, nums1: list[int], nums2: list[int]) -> list[int]:
        stack = []
        greater = {}
        for num in nums2:
            while stack and num > stack[-1]:
                greater[stack.pop()] = num
            stack.append(num)
        for num in stack:
            greater[num] = -1
        return [greater[num] for num in nums1]