-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-greater-element-i.cpp
More file actions
32 lines (27 loc) · 1 KB
/
Copy pathnext-greater-element-i.cpp
File metadata and controls
32 lines (27 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
//自己没想到O(n)的做法,这个单调栈需要借鉴 https://www.acwing.com/solution/LeetCode/content/399/
// 既和值有关系,又和位置有关系的 要考虑到单调栈的应用
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
unordered_map<int,int> um;
for(int i=0;i<nums.size();i++) um[nums[i]]=i;
stack<int> s; //单调栈
vector<int> greater(nums.size()); //要来存储第一个比自己大的下标
for(int i=0;i<nums.size();i++){
while(!s.empty() && nums[i]>nums[s.top()]){
greater[s.top()]=nums[i];
s.pop();
}
s.push(i);
}
while(!s.empty()){
greater[s.top()]=-1;
s.pop();
}
vector<int> vec(findNums.size());
for(int i=0;i<vec.size();i++){
vec[i]=greater[um[findNums[i]]];
}
return vec;
}
};