Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
- stack = [] # mono stack of elements seen so far
- next_greater = {}
- for i in range(len(nums2) - 1, -1, -1):
- n = nums2[i]
- while stack and stack[-1] <= n:
- stack.pop()
- next_greater[n] = stack[-1] if stack else -1
- stack.append(n)
- return [next_greater[n] for n in nums1]
Add Comment
Please, Sign In to add comment