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
- 1 <= nums1.length <= nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 10^4
- all values in
nums2are distinct, and every value ofnums1is innums2