Data Structures19 sections · 729 units
Open in Course

Problem - Next Greater Element I

Monotonic stack intro

You have two arrays nums1nums1 and nums2nums2 where nums1nums1 is a subset of nums2nums2. For each element in nums1nums1, find its next greater element in nums2nums2. Return 1-1 if there is none.

This problem has a twist: you need to find next greater elements in nums2nums2, then look up the answers for elements in nums1nums1. Use a monotonic stack on nums2nums2, store results in a hash map, then query for nums1nums1 elements.