Skip to content

Commit 8d12cc2

Browse files
authored
feat: add solutions to lc problem: No.1944 (#621)
No.1944. Number of Visible People in a Queue
1 parent d568735 commit 8d12cc2

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

solution/1900-1999/1944.Number of Visible People in a Queue/README.md

+40-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,31 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
单调栈。
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
67+
n = len(heights)
68+
ans = [0] * n
69+
stack = list()
70+
71+
for i in range(n - 1, -1, -1):
72+
while stack:
73+
ans[i] += 1;
74+
if heights[i] > stack[-1]:
75+
stack.pop()
76+
else:
77+
break
78+
stack.append(heights[i])
79+
80+
return ans
6481
```
6582

6683
### **Java**
@@ -71,10 +88,28 @@
7188

7289
```
7390

74-
### **...**
75-
76-
```
77-
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<int> canSeePersonsCount(vector<int>& heights) {
97+
int n = heights.size();
98+
vector<int> ans(n);
99+
stack<int> stk;
100+
for (int i = n - 1; i >= 0; --i)
101+
{
102+
while (!stk.empty())
103+
{
104+
ans[i]++;
105+
if (heights[i] <= stk.top()) break;
106+
stk.pop();
107+
}
108+
stk.push(heights[i]);
109+
}
110+
return ans;
111+
}
112+
};
78113
```
79114
80115
<!-- tabs:end -->

solution/1900-1999/1944.Number of Visible People in a Queue/README_EN.md

+40-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,29 @@ Person 5 can see no one since nobody is to the right of them.
4747

4848
## Solutions
4949

50+
Monotonic stack.
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

5456
```python
55-
57+
class Solution:
58+
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
59+
n = len(heights)
60+
ans = [0] * n
61+
stack = list()
62+
63+
for i in range(n - 1, -1, -1):
64+
while stack:
65+
ans[i] += 1;
66+
if heights[i] > stack[-1]:
67+
stack.pop()
68+
else:
69+
break
70+
stack.append(heights[i])
71+
72+
return ans
5673
```
5774

5875
### **Java**
@@ -61,10 +78,28 @@ Person 5 can see no one since nobody is to the right of them.
6178

6279
```
6380

64-
### **...**
65-
66-
```
67-
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
vector<int> canSeePersonsCount(vector<int>& heights) {
87+
int n = heights.size();
88+
vector<int> ans(n);
89+
stack<int> stk;
90+
for (int i = n - 1; i >= 0; --i)
91+
{
92+
while (!stk.empty())
93+
{
94+
ans[i]++;
95+
if (heights[i] <= stk.top()) break;
96+
stk.pop();
97+
}
98+
stk.push(heights[i]);
99+
}
100+
return ans;
101+
}
102+
};
68103
```
69104
70105
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> canSeePersonsCount(vector<int>& heights) {
4+
int n = heights.size();
5+
vector<int> ans(n);
6+
stack<int> stk;
7+
for (int i = n - 1; i >= 0; --i)
8+
{
9+
while (!stk.empty())
10+
{
11+
ans[i]++;
12+
if (heights[i] <= stk.top()) break;
13+
stk.pop();
14+
}
15+
stk.push(heights[i]);
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
3+
n = len(heights)
4+
ans = [0] * n
5+
stack = list()
6+
7+
for i in range(n - 1, -1, -1):
8+
while stack:
9+
ans[i] += 1;
10+
if heights[i] > stack[-1]:
11+
stack.pop()
12+
else:
13+
break
14+
stack.append(heights[i])
15+
16+
return ans

0 commit comments

Comments
 (0)