The next greater element of some element
You are given two distinct 0-indexed integer arrays
For each
Return an array
Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
4 is highlighted in nums2 = [1, 3,
1 is highlighted in nums2 = [
2 is highlighted in nums2 = [1, 3, 4,
Output: [3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
2 is highlighted in nums2 = [1,
4 is highlighted in nums2 = [1, 2, 3,
Contents
Solution 1 - scan through nums2 array for every number in nums1 Solution 2 - scan through nums2 and capture next possible greater element for every number (Answer to the follow-up question)
In this approach, we are going to loop through each number in
Complexity Analysis:
Time complexity: Above code runs in O(n * m) time where
Space complexity: O(m) for the answer array, in the above implementation, we are modifying input array
Main intution of this approach is that, we are going to loop through
Implementation details
If we have to capture next greater element for every number in
Since we are looking for next immediate greater element, there is a better way to capture that using a
-
We will be initializing a
Stack , which will be empty in the beginning and aHashMap<Integer, Integer> that we will be using to store the number from array as key and its next immediate greater element as value. -
Then, we are going to loop through the numbers in
nums2 array, and for each number innums2 arraycurrentNum check if this number is greater than the number at top of theStack :-
If it is, then add this to the
HashMap we have created,currentNum as key and number fromStack as value. -
If it is not greater than the number from
Stack , then add this number to theStack .
-
If it is, then add this to the
-
By the end of this loop,
HashMap will contain the next greater elements if it finds one, and theStack may still have some elements, if there is no greater element found. -
Then, for any remaining elements in
Stack , we simply add them to theHashMap with value as-1 since there is no greater element found. -
Finally, we can go through numbers in
nums1 array and get the next greater element that we have already computed fromHashMap and return the result.
Let's understand with an example, say
-
We will be looping through
nums2 and find next greater elements for each number in the array. -
In the first step
currentNum = 1 , since theStack is empty, we will just add this to the stack.Stack elements 1 Map key value -
Next number is
currentNum = 3 , now we look at the top element in theStack which is 1,currentNum is greater than the element coming from stack. So, for element1 immediate greater element is3 , so add it to the map. At the same time, we remove1 from stack, since we found answer for it, and add element3 to the stack.Stack elements 3 Map key value 1 3 -
Next number is
currentNum = 4 , now we look at the top element in theStack which is 3,currentNum is greater than the element coming from stack. So, for element3 immediate greater element is4 , so add it to the map. At the same time, we remove3 from stack, since we found answer for it, and add element4 to the stack.Stack elements 4 Map key value 1 3 3 4 -
Next number is
currentNum = 2 , now we look at the top element in theStack which is 4,currentNum is not greater than the element coming from stack. So, we simply add it to the stack.Stack elements 2 4 Map key value 1 3 3 4 -
We have now iterated through
nums2 aray fully. Now we look at stack and see if it still had any elements left, yes there are two elements left in the stack. So, for these elements we will add-1 to the map, this is because there are no greater elements found for these. Map now looks like this.Map key value 2 -1 4 -1 1 3 3 4 -
Now, we will go through
nums1 array and find the next greater element from the map we have prepared above. So, fornums1 = [4, 1, 2] , the result array is going to beresult = [-1, 3, -1] .
Complexity Analysis:
Time complexity: Above code runs in O(n + m) time where
Space complexity: O(n) for the
Above implementations source code can be found at