分析
- 题目:CM74 下一个较大元素
- 思路:保留最大元素,右下降栈
代码
class NextElement {
public:
vector<int> findNext(vector<int> A, int n) {
// write code here
vector<int> res(n);
stack<int> stk;
for(int i = n-1; i >= 0; i--){
while(stk.size() && A[stk.top()] <= A[i]) stk.pop();
if(stk.empty()) res[i] = -1;
else res[i] = A[stk.top()];
stk.push(i);
}
return res;
}
};