Monotonic decreasing stack. Pop when current is greater, store mapping.
function nextGreaterElement(nums1, nums2): nextGreater = {} stack = [] for num in nums2: while stack and num > stack[-1]: nextGreater[stack.pop()] = num stack.push(num) result = [] for num in nums1: result.append(nextGreater.get(num, -1)) return result
time, space.