Next Greater Element I
Given two distinct-valued arrays nums1 and nums2 where nums1 is a subset of nums2, for each element of nums1 find the next greater element to its right within nums2. Return an array where each position holds that next greater value, or -1 if none exists.
Open official problem prompt ↗For each queried value, report the first strictly larger number appearing to its right in the reference array, or -1.
People of different heights walk past a doorway. Each shorter person waits to note the first taller person who follows them; you jot each match in a notebook, then answer any 'who was next taller than X' question instantly.
- Input
- nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
- Output
- [-1, 3, -1]
- Why
- In nums2, 4 has nothing greater to its right (-1), 1's next greater is 3, and 2 has nothing greater to its right (-1).
1 <= nums1.length <= nums2.length <= 10000 <= nums1[i], nums2[i] <= 10^4All integers in nums1 and nums2 are uniqueAll integers of nums1 also appear in nums2