The algorithm: Create an empty stack and a hash map Traverse nums2 from right to left: - Pop all elements smaller than current (they're blocked) - Map current element to stack top (or if empty) - Push current element onto stack For each element in nums1, look up its next greater in the map The stack maintains a decreasing sequence of "candidates" for next greater.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/stacks/next-greater-element-i-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████████████████████████████████████████████████████████████████████████