496. Next Greater Element I

Practice Next Greater Element I for free on NudgeCode: Easy, Stack pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

nums2 holds distinct integers, and every value of nums1 also appears in nums2 (nums1 has no duplicates either). For each value x in nums1, find where x sits in nums2 and look to its right: the answer for x is the first value there that is strictly greater than x, or -1 if there is none. Return the answers in the order of nums1.

Example

nums1 = [4,1,2], nums2 = [1,3,4,2]  ->  [-1,3,-1]
nums1 = [2,4],   nums2 = [1,2,3,4]  ->  [3,-1]

Constraints