diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/README.md b/solution/1900-1999/1944.Number of Visible People in a Queue/README.md index 52da32fb574b7..f45c8e5afcbe9 100644 --- a/solution/1900-1999/1944.Number of Visible People in a Queue/README.md +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/README.md @@ -53,6 +53,8 @@ +单调栈。 + ### **Python3** @@ -60,7 +62,22 @@ ```python - +class Solution: + def canSeePersonsCount(self, heights: List[int]) -> List[int]: + n = len(heights) + ans = [0] * n + stack = list() + + for i in range(n - 1, -1, -1): + while stack: + ans[i] += 1; + if heights[i] > stack[-1]: + stack.pop() + else: + break + stack.append(heights[i]) + + return ans ``` ### **Java** @@ -71,10 +88,28 @@ ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n); + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { + ans[i]++; + if (heights[i] <= stk.top()) break; + stk.pop(); + } + stk.push(heights[i]); + } + return ans; + } +}; ``` diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/README_EN.md b/solution/1900-1999/1944.Number of Visible People in a Queue/README_EN.md index efc6f28bd6f2b..cb3f22cf35429 100644 --- a/solution/1900-1999/1944.Number of Visible People in a Queue/README_EN.md +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/README_EN.md @@ -47,12 +47,29 @@ Person 5 can see no one since nobody is to the right of them. ## Solutions +Monotonic stack. + ### **Python3** ```python - +class Solution: + def canSeePersonsCount(self, heights: List[int]) -> List[int]: + n = len(heights) + ans = [0] * n + stack = list() + + for i in range(n - 1, -1, -1): + while stack: + ans[i] += 1; + if heights[i] > stack[-1]: + stack.pop() + else: + break + stack.append(heights[i]) + + return ans ``` ### **Java** @@ -61,10 +78,28 @@ Person 5 can see no one since nobody is to the right of them. ``` -### **...** - -``` - +### **C++** + +```cpp +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n); + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { + ans[i]++; + if (heights[i] <= stk.top()) break; + stk.pop(); + } + stk.push(heights[i]); + } + return ans; + } +}; ``` diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp new file mode 100644 index 0000000000000..864140c0aff27 --- /dev/null +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n); + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { + ans[i]++; + if (heights[i] <= stk.top()) break; + stk.pop(); + } + stk.push(heights[i]); + } + return ans; + } +}; diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.py b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.py new file mode 100644 index 0000000000000..912285e63a6fa --- /dev/null +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def canSeePersonsCount(self, heights: List[int]) -> List[int]: + n = len(heights) + ans = [0] * n + stack = list() + + for i in range(n - 1, -1, -1): + while stack: + ans[i] += 1; + if heights[i] > stack[-1]: + stack.pop() + else: + break + stack.append(heights[i]) + + return ans \ No newline at end of file