text function nextGreaterElement(nums1, nums2): stack = empty stack nextGreater = empty hash map for i from length(nums2) - 1 down to 0: while stack not empty AND stack.top() <= nums2[i]: stack.pop() if stack is empty: nextGreater[nums2[i]] = -1 else: nextGreater[nums2[i]] = stack.top() stack.push(nums2[i]) result = [] for num in nums1: result.append(nextGreater[num]) return result Time complexity: . Space complexity: for stack and map.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/stacks/next-greater-element-i-pseudocode
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████████████████████████████████████████████████████████████████████████████████████████