Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2.
Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2.
If it does not exist, output -1 for this number.
1. All elements in nums1 and nums2 are unique.
2. The length of both nums1 and nums2 would not exceed 1000.
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Concept is that finding all elements' next greater element in the nums2 array.
Then, get the next greater element of nums1 by using the result we do last step.
We use a stack and a hashmap.
The stack is to store data in the ascending order in iterating the nums2 array.
The map is to store the greater element of every data in the nums2 array.
For every iteration to access nums2 array, when the read data is greater than the top of the stack, it means we find the next greater numbers of top element.
After finding the next greater number of the top element, we pop it and put it into map, recursively.
Finally, we use the map to get the greater element of elements in nums1.
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> stack = new Stack<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int len1 = nums1.length;
int len2 = nums2.length;
int[] result = new int[len1];
for (int i = 0; i < len2; i++) {
int val = nums2[i];
while (!stack.isEmpty() && stack.peek() < val) {
map.put(stack.pop(), val);
}
stack.push(val);
}
for (int i = 0; i < len1; i++) {
result[i] = map.getOrDefault(nums1[i], -1);
}
return result;
}
}
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
stack<int> smalls;
map<int, int> greaters;
int len1 = findNums.size();
int len2 = nums.size();
vector<int> result;
for (int i = 0; i < len2; i++) {
int val = nums[i];
while (!smalls.empty() && smalls.top() < val) {
greaters[smalls.top()] = val;
smalls.pop();
}
smalls.push(val);
}
for (int i = 0; i < len1; i++) {
if (greaters.find(findNums[i]) != greaters.end()) {
result.push_back(greaters[findNums[i]]);
} else {
result.push_back(-1);
}
}
return result;
}
};
class Solution(object):
def nextGreaterElement(self, findNums, nums):
"""
:type findNums: List[int]
:type nums: List[int]
:rtype: List[int]
"""
smalls = []
greaters = {}
result = []
for val in nums:
while len(smalls) != 0 and smalls[len(smalls) - 1] < val:
greaters[smalls[len(smalls) - 1]] = val
del(smalls[len(smalls) - 1])
smalls.append(val)
for val in findNums:
if val not in greaters.keys():
result.append(-1)
else:
result.append(greaters[val])
return result
- Time Complexity: O(N^2), N is the length of nums2
- Space Complexity: O(M + N), M is the length of nums1, N is the length of nums2