This algorithm takes as input a vector of integers and will store the max first, let's stay its position is p in the array, then we store the max element in the range [p, n[ and so on.
// input v[0]...v[n-1] is a vector of integers already filled.
// output : stack s
stack<int> s;
for(auto x: v){
while(!s.empty() && x > s.top()) s.pop();
s.push(x);
}
I can't figure out the worst case, but it seems the algorithm is linear, because the more the while() will work the more the stack size will be reduced and make the future work very cheap.