Next Greater Element I
Problem statement
You are given two arrays of distinct integers, query and reference, where every query value appears in reference.
For each query value, return the first greater value to its right in reference, or -1 when none exists.
Function
nextGreaterElement(query: int[], reference: int[]) → int[]Examples
Example 1
query = [4,1,2]reference = [1,3,4,2]return = [-1,3,-1]Four and two have no greater value to their right; one is followed by three.
Example 2
query = [2,4]reference = [1,2,3,4]return = [3,-1]Three is the next greater value after two, while four has none.
Constraints
1 <= query.length <= reference.length <= 10^5.- All values in each array are distinct.
- Every value in
queryappears inreference.
Source note: Author-written prompt excerpt retained for source fidelity.