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;
    }
};

猜你喜欢

转载自blog.csdn.net/SYaoJun/article/details/120683277