Next Greater Element I — Monotonic Stack
For each building along a street, report the height of the nearest taller one to its right, or a marker if there is none.
Monotonic Stack · Easy
The problem
A street has buildings of distinct heights in a row (nums2), and you care about a few of them (nums1). You get two lists of distinct integers, where every value of nums1 also appears in nums2. For each value of nums1, look at where it sits in nums2 and find the first strictly larger value somewhere to its right.
The story
Along a street of buildings, every building has a different height. An estate agent has a shortlist of some of those buildings and, for each one, wants the height of the first taller building to its right, the one that will block its sunset view. If nothing to its right is taller, report -1 for that building.
What to return
- Return a list with one entry per element of nums1, in the same order.
- Each entry is the first value to the right of that element inside nums2 that is strictly greater than it, or -1 if there is none.
Constraints
- 1 <= nums2.length <= 500; values of nums2 are distinct integers in 0..1000
- 1 <= nums1.length <= nums2.length; values of nums1 are distinct
- every value of nums1 appears in nums2
- Your code may run at most 11,960 counted steps on the largest case.
Example
- Input
nums1 = [3, 7, 2], nums2 = [7, 2, 10, 3, 5]- Output
[5, 10, 10]