#include <stack>
#include <vector>
class NextElement {
public:
vector<int> findNext(vector<int> A, int n) {
// write code here
vector<int> res(n, -1);
stack<int> index_stk;
for (int i = 0; i < n; i++) {
while (!index_stk.empty() && A[i] > A[index_stk.top()]) {
res[index_stk.top()] = A[i];
index_stk.pop();
}
index_stk.push(i);
}
return res;
}
};

京公网安备 11010502036488号