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 <= 1000
0 <= nums1[i], nums2[i] <= 104
nums1
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:
We want to find the next bigger number for each number in a smaller set, compared to a larger set. The brute force method is all about looking at every single possibility to find the answer. We check each number in the smaller set against every number in the larger set until we find a bigger one.
Here's how the algorithm would work step-by-step:
def next_greater_element_i_brute_force(nums1, nums2):
result = []
for number_from_smaller_set in nums1:
next_greater_number = -1
for number_from_larger_set in nums2:
# Finding the next greater number
if number_from_larger_set > number_from_smaller_set:
next_greater_number = number_from_larger_set
break
result.append(next_greater_number)
return result
The key idea is to use a temporary holding space to keep track of potentially greater numbers we've seen but haven't found a match for yet. This lets us quickly find the next greater number for each number we encounter without repeatedly searching.
Here's how the algorithm would work step-by-step:
def next_greater_element(nums1, nums2):
next_greater = {}
temporary_storage = []
# Iterate to find next greater elements
for number in nums2:
# Find next greater element for previous elements
while temporary_storage and number > temporary_storage[-1]:
next_greater[temporary_storage.pop()] = number
temporary_storage.append(number)
# Numbers left in stack have no greater element
while temporary_storage:
next_greater[temporary_storage.pop()] = -1
result = []
# Build results using next greater elements
for number in nums1:
result.append(next_greater[number])
return result
Case | How to Handle |
---|---|
nums1 or nums2 is null or empty | Return an empty array if either input array is null or empty. |
nums1 is larger than nums2 | The algorithm must still function correctly by only searching through nums2 to find next greater elements for nums1. |
nums1 and nums2 contain duplicate values | The stack and hash map will correctly handle duplicates by storing the last seen index/value. |
No greater element exists for some elements in nums1 | Initialize the result array with -1, which will remain for elements without a greater element. |
nums2 contains a decreasing sequence | The stack will only contain decreasing elements and pop them as soon as a greater element is encountered. |
nums1 and nums2 contain negative numbers | The algorithm should work correctly with negative numbers without any modifications. |
Maximum size arrays exceeding memory constraints | Ensure algorithm uses linear memory and avoids unnecessary copying to prevent memory issues. |
Integer overflow possible if elements are very large | The comparison operations and data storage should use appropriate data types (e.g., int) to prevent overflow when dealing with large numbers. |