leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1944.cpp (513B)
0 class Solution { 1 public: 2 vector<int> canSeePersonsCount(const vector<int> &heights) const { 3 const int n = size(heights); 4 vector<int> res(n); 5 stack<int> st; 6 7 for (int i = n - 1; i >= 0; i--) { 8 const int h = heights[i]; 9 int run = 0; 10 11 while (!st.empty() && h >= st.top()) { 12 st.pop(); 13 run++; 14 } 15 16 res[i] = run + !st.empty(); 17 st.push(h); 18 } 19 20 return res; 21 } 22 };