dsa · easy
Next Greater Element I
The next greater element of a value x in nums2 is the first strictly greater value to the **right** of x in nums2. If none exists, it is -1.
Arguments
nums1— query values; each appears exactly once in nums2nums2— scan rightward from each query value for the first strictly greater element
nums1 is a subset of nums2. All values in nums1 and in nums2 are unique, so each value in nums1 appears at exactly one index of nums2.
For each value in nums1, return its next greater element in nums2.
Example
nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2] → [-1, 3, -1].
4innums2has only2to its right, which is not greater →-11innums2has3to its right →32is last innums2→-1
Constraints
1 <= nums1.length <= nums2.length <= 1000 0 <= nums1[i], nums2[i] <= 10^4 All integers in nums1 and nums2 are unique Every integer of nums1 also appears in nums2 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [4,1,2] [1,3,4,2] Expected: [-1,3,-1]
Example 2
Input: [2,4] [1,2,3,4] Expected: [3,-1]