牛客编程巅峰赛 第十场第二题

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * @param n int整型
     * @param a int整型vector
     * @return int整型
     */
int wwork(int n, vector<int>& a) {
        // write code here
        int counts = 0;
        stack<int> st;
        for (int i = 0; i < n;++i) {
            if (st.empty()) {
                st.push(a[i]);
            }
            else if(st.top()<a[i]) {
                st.push(a[i]);
            }
            else {
                while (!st.empty()&&st.top() > a[i]) {
                    st.pop();
                    ++counts;
                }
                st.push(a[i]);
            }
        }
        return counts;
    }
};


#题解#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务