Next greater element I

August 14, 2022

stack

Problem URL: Next greater element I

We traverse nums2 and start storing elements on the top of stack. If current number is greater than the top of the stack, then we found a pair. Keep popping from stack till the top of stack is smaller than current number. After matching pairs are found, push current number on top of stack. Eventually when there are no more elements in nums2 to traverse, but there are elements in stack, we can justify that next bigger element wasn't found for them. Hence we'll put -1 for the remaining elements in stack.

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

        return [mapping.get(n, -1) for n in nums1]

Time Complexity: O(n*m), n is length for nums1 and m is the length of nums2.
Space Complexity: O(m)