You are given two lists of distinct integers, queries and nums. Every value in queries also appears in nums.
For a value x at index j of nums, its next greater value is the first value to the right of index j that is strictly greater than x.
Return a list result of the same length as queries, where result[i] is the next greater value of queries[i] within nums. If there is no such value, result[i] is -1.
Examples
Example 1
Input: queries = [4, 1, 2], nums = [1, 3, 4, 2]
Output: [-1, 3, -1]
Explanation: Nothing to the right of 4 is greater, so its answer is -1. The first value to the right of 1 that is greater is 3. Nothing lies to the right of 2, so its answer is -1.
Example 2
Input: queries = [2, 4], nums = [1, 2, 3, 4]
Output: [3, -1]
Explanation: The first value to the right of 2 that is greater is 3. Nothing lies to the right of 4, so its answer is -1.
Constraints
1≤queries.length≤nums.length≤1000
0≤queries[i], nums[i]≤104
All values in queries are distinct and all values in nums are distinct.
Every value in queries appears in nums.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up