496. Next Greater Element I
The question comes from leetcode, and the solution and ideas represent only my personal opinions. Portal.
Difficulty: Easy
Time: 10min
topic
Give you two arrays nums1
and nums2
without duplicate elements, where nums1
is < a subset of i=4>. nums2
Please find the next greater value of each element in nums1
in nums2
.
nums1
x
The next larger element of the number in at the corresponding position. . If it does not exist, output An element larger than x
in is the element to the right of the corresponding position in nums2
x
-1
Example 1:
输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
对于 num1 中的数字 4 ,你无法在第二个数组中找到下一个更大的数字,因此输出 -1 。
对于 num1 中的数字 1 ,第二个数组中数字1右边的下一个较大数字是 3 。
对于 num1 中的数字 2 ,第二个数组中没有下一个更大的数字,因此输出 -1 。
Example 2:
输入: nums1 = [2,4], nums2 = [1,2,3,4].
输出: [3,-1]
解释:
对于 num1 中的数字 2 ,第二个数组中的下一个较大数字是 3 。
对于 num1 中的数字 4 ,第二个数组中没有下一个更大的数字,因此输出 -1 。
Hint: All the integers in
1 <= nums1.length <= nums2.length <= 1000
0 <= nums1[i], nums2[i] <=10^4
nums1
andnums2
are different from each other All integers in also appear innums1
nums2
Advanced: Can you design a solution with time complexity O(nums1.length + nums2.length)
?
Ideas
- Violent solution. I won’t write this anymore.
- Monotone stack + hash table.
Monotone stack + hash table
In fact, this is a type of question - monotonic stack. So record it.
Identification keywords:
Next, bigger, smaller
Because,
nums1
is a subset of nums2
.
nums1
and nums
are both unordered.
The result is the next larger element of all elements in nums1.
Then,
we need to use a map
recordnums1
element of The next larger element.
Moreover, using a monotonic stack, we can find the next greater elementnums2
of all elements in .
Monotonic stack operation process (finding the next larger number):
For all elements of nums2
:
- If the stack is empty, push it
当前元素
. - If the stack is not empty,
栈顶元素
is compared with当前元素
.
- Result
栈顶元素
>=当前元素
Entering当前元素
. - If
栈顶元素
<当前元素
(当前元素
is,栈顶元素
a>The next larger element)
Execute the loop and pop all elements smaller than当前元素
from the stack element (itsnext larger element is also当前元素
).
code
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
int left = 0;
int rihgt = 0;
int n1 = nums1.size();
int n2 = nums2.size();
vector<int> ans(n1,-1);
unordered_map<int,int> mmap;
stack<int> s;
for(int i=0;i<n2;i++){
if(s.empty()){
s.push(nums2[i]);
}else if(s.top() < nums2[i]){
while(!s.empty() && s.top() < nums2[i]){
int num = s.top();
s.pop();
mmap[num] = nums2[i];
}
s.push(nums2[i]);
}else if(s.top() >= nums2[i]){
s.push(nums2[i]);
}
}
for(int i=0;i<n1;i++){
if(mmap.count(nums1[i]) == 0){
continue;
}
ans[i] = mmap[nums1[i]];
}
return ans;
}
};
Optimize structural code
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
int left = 0;
int rihgt = 0;
int n1 = nums1.size();
int n2 = nums2.size();
vector<int> ans(n1,-1);
unordered_map<int,int> mmap;
stack<int> s;
for(int i=0;i<n2;i++){
while(!s.empty() && s.top() < nums2[i]){
int num = s.top();
s.pop();
mmap[num] = nums2[i];
}
s.push(nums2[i]);
}
for(int i=0;i<n1;i++){
if(mmap.count(nums1[i]) == 0){
continue;
}
ans[i] = mmap[nums1[i]];
}
return ans;
}
};
Algorithmic complexity
Time efficiency: O ( n 1 + n 2 ) O(n_1+n_2) O(n1+n2), inside n 1 n_1 n1为nums1
's length, n 2 n_2 n2为nums2
's length. O ( n 1 ) O(n_1) O(n1)Using the generated answer, O ( n 2 ) O(n_2) O(n2)usesearch for the single largest element.
Space power: O ( n 1 + n 2 ) O(n_1+n_2) O(n1+n2), inside n 1 n_1 n1为nums1
's length, n 2 n_2 n2为nums2
's length. O ( n 1 ) O(n_1) O(n1)用于哈子表, O ( n 2 ) O(n_2) O(n2)Using the training station.