Posted on

Description

Submission

class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& heights) {
        stack<int> stk;
        int n = heights.size();

        vector<int> rets(n);
        for(int i = n - 1; i >= 0; --i) {
            int cnt = 0;
            while(!stk.empty() && stk.top() < heights[i]) {
                stk.pop();
                ++cnt;
            }
            if(!stk.empty()) ++cnt;
            stk.push(heights[i]);

            rets[i] = cnt;
        }
        return rets;
    }
};

// 10 6 8 5 11 9
    
//     9 11 -> 11
//     11 5


// decreasing stack

Leave a Reply

Your email address will not be published. Required fields are marked *