Description

Given two arrays nums1 (subset of nums2), find the next greater element for each element in nums1 in nums2. Return -1 if no greater element exists.

Examples

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

Next greater for 4 is none, for 1 is 3, for 2 is none.

Input:nums1 = [6,8,5], nums2 = [5,6,7,8,9]
Output:[7,9,6]
Explanation:

For 6: the next greater element is 7. For 8: the next greater element is 9. For 5: the next greater element is 6.

Input:nums1 = [10], nums2 = [15,10,20,5]
Output:[20]
Explanation:

For the single element 10 in nums1: looking at nums2, after 10 comes 20 and 5. Since 20 > 10, the next greater element is 20.

Constraints

  • 1 ≤ nums1.length ≤ nums2.length ≤ 1000
  • 0 ≤ nums1[i], nums2[i] ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!