LeetCode 496 Next Greater Element I - Problem Statement

The problem

Given two arrays nums1 and nums2 where nums1 is a subset of nums2, find the next greater element for each element in nums1.

The next greater element of x in nums2 is the first element to its right that is larger. Return -1 if none exists.

With nums1 = [4,1,2] and nums2 = [1,3,4,2]:

  • For 4: nothing greater to the right. Answer: -1.
  • For 1: next greater is 3. Answer: 3.
  • For 2: nothing greater to the right. Answer: -1.

Result: [-1, 3, -1].

Constraints: 11 \le length 1000\le 1000. All elements unique.