From a5f39bb810eebeceaa5d9821b7db812395a7d1aa Mon Sep 17 00:00:00 2001 From: imp Date: Sat, 4 Dec 2021 19:58:08 +0800 Subject: [PATCH 1/5] feat: add solutions to lc problem: No.1944 --- .../README.md | 48 ++++++++++++++++-- .../README_EN.md | 49 +++++++++++++++++-- .../Solution.cpp | 23 +++++++++ .../Solution.py | 16 ++++++ 4 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp create mode 100644 solution/1900-1999/1944.Number of Visible People in a Queue/Solution.py 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..60d3321a26557 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** @@ -69,12 +86,35 @@ ```java -``` - -### **...** ``` +### **C++** + +```cpp +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n); + stack st; + + for(int i = n - 1; i >= 0; --i) { + while(!st.empty()) { + ans[i]++; + if(heights[i] > st.top()) { + st.pop(); + } + + else { + break; + } + } + st.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..f3e3ed4e96a03 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,32 @@ 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 st; + + for(int i = n - 1; i >= 0; --i) { + while(!st.empty()) { + ans[i]++; + if(heights[i] > st.top()) { + st.pop(); + } + + else { + break; + } + } + st.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..41fedb20a4dfc --- /dev/null +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector canSeePersonsCount(vector& heights) { + int n = heights.size(); + vector ans(n); + stack st; + + for(int i = n - 1; i >= 0; --i) { + while(!st.empty()) { + ans[i]++; + if(heights[i] > st.top()) { + st.pop(); + } + + else { + break; + } + } + st.push(heights[i]); + } + return ans; + } +}; \ No newline at end of file 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 From 97634b3c4faf8adf828bb5d1a90460398ea4c4d2 Mon Sep 17 00:00:00 2001 From: imp Date: Sat, 4 Dec 2021 20:00:34 +0800 Subject: [PATCH 2/5] fix: reduction --- .../1900-1999/1944.Number of Visible People in a Queue/README.md | 1 - 1 file changed, 1 deletion(-) 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 60d3321a26557..17037148c6869 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 @@ -86,7 +86,6 @@ class Solution: ```java - ``` ### **C++** From 15bef55e970b1e5dd2428376c18e7a9b5c3ba238 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 4 Dec 2021 20:16:02 +0800 Subject: [PATCH 3/5] Update README.md --- .../README.md | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) 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 17037148c6869..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 @@ -96,20 +96,16 @@ public: vector canSeePersonsCount(vector& heights) { int n = heights.size(); vector ans(n); - stack st; - - for(int i = n - 1; i >= 0; --i) { - while(!st.empty()) { + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { ans[i]++; - if(heights[i] > st.top()) { - st.pop(); - } - - else { - break; - } + if (heights[i] <= stk.top()) break; + stk.pop(); } - st.push(heights[i]); + stk.push(heights[i]); } return ans; } From 0fb69227cf74d57833c78122293378c1e8f41b6e Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 4 Dec 2021 20:18:46 +0800 Subject: [PATCH 4/5] Update README_EN.md --- .../README_EN.md | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) 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 f3e3ed4e96a03..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 @@ -86,20 +86,16 @@ public: vector canSeePersonsCount(vector& heights) { int n = heights.size(); vector ans(n); - stack st; - - for(int i = n - 1; i >= 0; --i) { - while(!st.empty()) { + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { ans[i]++; - if(heights[i] > st.top()) { - st.pop(); - } - - else { - break; - } + if (heights[i] <= stk.top()) break; + stk.pop(); } - st.push(heights[i]); + stk.push(heights[i]); } return ans; } From 50d955ee331eab0b998d2bcf7636b508c46a8e1c Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sat, 4 Dec 2021 20:19:04 +0800 Subject: [PATCH 5/5] Update Solution.cpp --- .../Solution.cpp | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) 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 index 41fedb20a4dfc..864140c0aff27 100644 --- 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 @@ -3,21 +3,17 @@ class Solution { vector canSeePersonsCount(vector& heights) { int n = heights.size(); vector ans(n); - stack st; - - for(int i = n - 1; i >= 0; --i) { - while(!st.empty()) { + stack stk; + for (int i = n - 1; i >= 0; --i) + { + while (!stk.empty()) + { ans[i]++; - if(heights[i] > st.top()) { - st.pop(); - } - - else { - break; - } + if (heights[i] <= stk.top()) break; + stk.pop(); } - st.push(heights[i]); + stk.push(heights[i]); } return ans; } -}; \ No newline at end of file +};