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

Constraints

Example

Input
nums1 = [3, 7, 2], nums2 = [7, 2, 10, 3, 5]
Output
[5, 10, 10]

Solve it

More in Monotonic Stack

All algorithm problems