Next Greater Element I
Easy · stack
You are given a list nums of distinct integers, and a list queries of values. Every value in queries also appears in nums.
For each value x in queries, look at the numbers that sit to the right of x in nums, from left to right, and find the first one that is strictly larger than x.
Return a list with one entry per query, in the same order as queries. The entry is that larger number (the value, not its position), or -1 if nothing to the right of x is larger.
Examples
Example 1
Input: nums = [6, 2, 9, 4, 7, 3], queries = [4, 6, 3]
Output: [7, 9, -1]
Right of 4 comes 7. Right of 6 the first bigger value is 9. Nothing follows 3, so -1.
Example 2
Input: nums = [5, 4, 3, 2, 1], queries = [3, 5]
Output: [-1, -1]
The list only goes down, so no value has a larger one to its right.
Example 3
Input: nums = [1, 8, 3, 5], queries = [3, 1, 3]
Output: [5, 8, 5]
Queries may repeat, and each one gets its own answer.
Constraints
- 1 <= len(nums) <= 10^4
- 1 <= len(queries) <= len(nums)
- 0 <= nums[i] <= 10^9, and all values in nums are distinct
- Every value in queries appears in nums (a value may be queried more than once).