The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1] Explanation: The next greater element for each value of nums1 is as follows: - 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. - 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3. - 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4] Output: [3,-1] Explanation: The next greater element for each value of nums1 is as follows: - 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3. - 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
Constraints:
1 <= nums1.length <= nums2.length <= 10000 <= nums1[i], nums2[i] <= 104nums1 and nums2 are unique.nums1 also appear in nums2.O(nums1.length + nums2.length) solution?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the next greater element involves exhaustively checking every possible option for each item in our smaller group of numbers. We simply look at the larger group of numbers to see what comes next for each item we care about.
Here's how the algorithm would work step-by-step:
def next_greater_element(subset_numbers, complete_numbers):
answer_results = []
# Iterate through each query item to find its specific next greater value in the wider array
for current_number in subset_numbers:
target_index = -1
for search_index in range(len(complete_numbers)):
if complete_numbers[search_index] == current_number:
target_index = search_index
break
found_greater = -1
# Scan only the elements to the right of the matched position to respect array boundaries
for right_index in range(target_index + 1, len(complete_numbers)):
if complete_numbers[right_index] > current_number:
found_greater = complete_numbers[right_index]
break
# Append the discovered greater value or a fallback indicator if none exists
answer_results.append(found_greater)
return answer_resultsTo efficiently find the next greater element for a group of numbers, we can scan through the complete collection of numbers while keeping a running track of items waiting for a larger partner. By processing the full sequence ahead of time, we can instantly look up the answer for any specific number without having to search through the collection repeatedly.
Here's how the algorithm would work step-by-step:
def next_greater_element(subset_numbers, complete_collection):
monotonic_stack = []
greater_mapping = {}
# Traverse right to left to precompute the next greater element for each value using a stack.
for current_number in reversed(complete_collection):
while monotonic_stack and monotonic_stack[-1] <= current_number:
monotonic_stack.pop()
if monotonic_stack:
greater_mapping[current_number] = monotonic_stack[-1]
else:
greater_mapping[current_number] = -1
monotonic_stack.append(current_number)
result_collection = []
# Look up precomputed results for the subset numbers to build the final answer efficiently.
for target_number in subset_numbers:
result_collection.append(greater_mapping.get(target_number, -1))
return result_collection| Case | How to Handle |
|---|---|
| Empty input arrays for nums1 or nums2 | Return an empty array immediately since no lookup operations can be performed. |
| Minimum-sized arrays where both nums1 and nums2 contain exactly one element | The algorithm will correctly map the single element to -1 because there are no elements to its right. |
| Strictly decreasing array in nums2 such as [5, 4, 3, 2, 1] | The monotonic stack will empty out without finding any greater elements, resulting in -1 for all queries. |
| Strictly increasing array in nums2 such as [1, 2, 3, 4, 5] | Each element will successfully find its immediate right neighbor as the next greater element. |
| Arrays containing negative numbers, zeros, and mixed integer ranges | The hash map and comparison logic work natively with signed integers without requiring special transformations. |
| Maximum-sized inputs running into constraints where nums2 is very large | A linear time monotonic stack and hash map approach ensures the solution scales efficiently within O(N + M) time complexity. |
| Elements in nums1 appearing at the very last index of nums2 | The precomputed hash map will store -1 for these elements since no elements exist to their right. |
| Duplicate values within the inputs | The problem statement specifies that all integers in nums1 and nums2 are distinct, so strict uniqueness is guaranteed. |