Next Greater Element I

Stacks & Monotonic Stacks, problem 2 of 7

Next Greater Element I

Easy

LC #496

monotonic stackhash map

Not attempted yet

You get two arrays of distinct integers, nums1 and nums2, where every value of nums1 also appears in nums2.

For a value x, its next greater element is the first value to the right of x in nums2 that is larger than x (or -1 if there is none).

Return a list with the next greater element of each value of nums1, in the order of nums1.

Example 1

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Explanation: nothing bigger follows 4 or 2;
3 is the first value after 1 that beats it.

Example 2

Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]

Example 3

Input: nums1 = [1,3,5,2,4],
       nums2 = [6,5,4,3,2,1,7]
Output: [7,7,7,7,7]

Constraints

  • 1 <= len(nums1) <= len(nums2) <= 3 * 10^4
  • values are distinct integers
  • every value of nums1 is in nums2

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.