kosievdmerwe

Untitled

Oct 21st, 2021 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
  3.         stack = []  # mono stack of elements seen so far
  4.         next_greater = {}
  5.        
  6.         for i in range(len(nums2) - 1, -1, -1):
  7.             n = nums2[i]
  8.            
  9.             while stack and stack[-1] <= n:
  10.                 stack.pop()
  11.                
  12.             next_greater[n] = stack[-1] if stack else -1
  13.             stack.append(n)
  14.            
  15.         return [next_greater[n] for n in nums1]
Add Comment
Please, Sign In to add comment