leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1944.cpp (513B)


      1 class Solution {
      2   public:
      3     vector<int> canSeePersonsCount(const vector<int> &heights) const {
      4         const int n = size(heights);
      5         vector<int> res(n);
      6         stack<int> st;
      7 
      8         for (int i = n - 1; i >= 0; i--) {
      9             const int h = heights[i];
     10             int run = 0;
     11 
     12             while (!st.empty() && h >= st.top()) {
     13                 st.pop();
     14                 run++;
     15             }
     16 
     17             res[i] = run + !st.empty();
     18             st.push(h);
     19         }
     20 
     21         return res;
     22     }
     23 };