Skip to content

Commit bd9c9c7

Browse files
authored
feat: add solutions to lc problem: No.1944 (doocs#1524)
No.1944.Number of Visible People in a Queue
1 parent dafd02d commit bd9c9c7

File tree

7 files changed

+211
-92
lines changed

7 files changed

+211
-92
lines changed

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

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,19 @@
5252

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

55-
单调栈。
55+
**方法一:单调栈**
56+
57+
我们观察发现,对于第 $i$ 个人来说,他能看到的人一定是按从左到右高度严格单调递增的。
58+
59+
因此,我们可以倒序遍历数组 $heights$,用一个从栈顶到栈底单调递增的栈 $stk$ 记录已经遍历过的人的高度。
60+
61+
对于第 $i$ 个人,如果栈不为空并且栈顶元素小于 $heights[i]$,累加当前第 $i$ 个人能看到的人数,然后将栈顶元素出栈,直到栈为空或者栈顶元素大于等于 $heights[i]$。如果此时栈不为空,说明栈顶元素大于等于 $heights[i]$,那么第 $i$ 个人能看到的人数还要再加 $1$。
62+
63+
接下来,我们将 $heights[i]$ 入栈,继续遍历下一个人。
64+
65+
遍历结束后,返回答案数组 $ans$。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $heights$ 的长度。
5668

5769
<!-- tabs:start -->
5870

@@ -65,17 +77,14 @@ class Solution:
6577
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
6678
n = len(heights)
6779
ans = [0] * n
68-
stack = list()
69-
80+
stk = []
7081
for i in range(n - 1, -1, -1):
71-
while stack:
82+
while stk and stk[-1] < heights[i]:
7283
ans[i] += 1
73-
if heights[i] > stack[-1]:
74-
stack.pop()
75-
else:
76-
break
77-
stack.append(heights[i])
78-
84+
stk.pop()
85+
if stk:
86+
ans[i] += 1
87+
stk.append(heights[i])
7988
return ans
8089
```
8190

@@ -84,7 +93,24 @@ class Solution:
8493
<!-- 这里可写当前语言的特殊实现逻辑 -->
8594

8695
```java
87-
96+
class Solution {
97+
public int[] canSeePersonsCount(int[] heights) {
98+
int n = heights.length;
99+
int[] ans = new int[n];
100+
Deque<Integer> stk = new ArrayDeque<>();
101+
for (int i = n - 1; i >= 0; --i) {
102+
while (!stk.isEmpty() && stk.peek() < heights[i]) {
103+
stk.pop();
104+
++ans[i];
105+
}
106+
if (!stk.isEmpty()) {
107+
++ans[i];
108+
}
109+
stk.push(heights[i]);
110+
}
111+
return ans;
112+
}
113+
}
88114
```
89115

90116
### **C++**
@@ -96,35 +122,58 @@ public:
96122
int n = heights.size();
97123
vector<int> ans(n);
98124
stack<int> stk;
99-
for (int i = n - 1; i >= 0; --i) {
100-
while (!stk.empty()) {
101-
ans[i]++;
102-
if (heights[i] <= stk.top()) break;
125+
for (int i = n - 1; ~i; --i) {
126+
while (stk.size() && stk.top() < heights[i]) {
127+
++ans[i];
103128
stk.pop();
104129
}
130+
if (stk.size()) {
131+
++ans[i];
132+
}
105133
stk.push(heights[i]);
106134
}
107135
return ans;
108136
}
109137
};
110138
```
111139
140+
### **Go**
141+
142+
```go
143+
func canSeePersonsCount(heights []int) []int {
144+
n := len(heights)
145+
ans := make([]int, n)
146+
stk := []int{}
147+
for i := n - 1; i >= 0; i-- {
148+
for len(stk) > 0 && stk[len(stk)-1] < heights[i] {
149+
ans[i]++
150+
stk = stk[:len(stk)-1]
151+
}
152+
if len(stk) > 0 {
153+
ans[i]++
154+
}
155+
stk = append(stk, heights[i])
156+
}
157+
return ans
158+
}
159+
```
160+
112161
### **TypeScript**
113162

114163
```ts
115164
function canSeePersonsCount(heights: number[]): number[] {
116165
const n = heights.length;
117-
const ans = new Array(n).fill(0);
118-
const stack = [];
119-
for (let i = n - 1; i >= 0; i--) {
120-
while (stack.length !== 0) {
121-
ans[i]++;
122-
if (heights[i] <= heights[stack[stack.length - 1]]) {
123-
break;
124-
}
125-
stack.pop();
166+
const ans: number[] = new Array(n).fill(0);
167+
const stk: number[] = [];
168+
for (let i = n - 1; ~i; --i) {
169+
while (stk.length && stk.at(-1) < heights[i]) {
170+
++ans[i];
171+
stk.pop();
172+
}
173+
if (stk.length) {
174+
++ans[i];
126175
}
127-
stack.push(i);
176+
stk.push(heights[i]);
128177
}
129178
return ans;
130179
}

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

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,38 @@ class Solution:
5757
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
5858
n = len(heights)
5959
ans = [0] * n
60-
stack = list()
61-
60+
stk = []
6261
for i in range(n - 1, -1, -1):
63-
while stack:
62+
while stk and stk[-1] < heights[i]:
6463
ans[i] += 1
65-
if heights[i] > stack[-1]:
66-
stack.pop()
67-
else:
68-
break
69-
stack.append(heights[i])
70-
64+
stk.pop()
65+
if stk:
66+
ans[i] += 1
67+
stk.append(heights[i])
7168
return ans
7269
```
7370

7471
### **Java**
7572

7673
```java
77-
74+
class Solution {
75+
public int[] canSeePersonsCount(int[] heights) {
76+
int n = heights.length;
77+
int[] ans = new int[n];
78+
Deque<Integer> stk = new ArrayDeque<>();
79+
for (int i = n - 1; i >= 0; --i) {
80+
while (!stk.isEmpty() && stk.peek() < heights[i]) {
81+
stk.pop();
82+
++ans[i];
83+
}
84+
if (!stk.isEmpty()) {
85+
++ans[i];
86+
}
87+
stk.push(heights[i]);
88+
}
89+
return ans;
90+
}
91+
}
7892
```
7993

8094
### **C++**
@@ -86,35 +100,58 @@ public:
86100
int n = heights.size();
87101
vector<int> ans(n);
88102
stack<int> stk;
89-
for (int i = n - 1; i >= 0; --i) {
90-
while (!stk.empty()) {
91-
ans[i]++;
92-
if (heights[i] <= stk.top()) break;
103+
for (int i = n - 1; ~i; --i) {
104+
while (stk.size() && stk.top() < heights[i]) {
105+
++ans[i];
93106
stk.pop();
94107
}
108+
if (stk.size()) {
109+
++ans[i];
110+
}
95111
stk.push(heights[i]);
96112
}
97113
return ans;
98114
}
99115
};
100116
```
101117
118+
### **Go**
119+
120+
```go
121+
func canSeePersonsCount(heights []int) []int {
122+
n := len(heights)
123+
ans := make([]int, n)
124+
stk := []int{}
125+
for i := n - 1; i >= 0; i-- {
126+
for len(stk) > 0 && stk[len(stk)-1] < heights[i] {
127+
ans[i]++
128+
stk = stk[:len(stk)-1]
129+
}
130+
if len(stk) > 0 {
131+
ans[i]++
132+
}
133+
stk = append(stk, heights[i])
134+
}
135+
return ans
136+
}
137+
```
138+
102139
### **TypeScript**
103140

104141
```ts
105142
function canSeePersonsCount(heights: number[]): number[] {
106143
const n = heights.length;
107-
const ans = new Array(n).fill(0);
108-
const stack = [];
109-
for (let i = n - 1; i >= 0; i--) {
110-
while (stack.length !== 0) {
111-
ans[i]++;
112-
if (heights[i] <= heights[stack[stack.length - 1]]) {
113-
break;
114-
}
115-
stack.pop();
144+
const ans: number[] = new Array(n).fill(0);
145+
const stk: number[] = [];
146+
for (let i = n - 1; ~i; --i) {
147+
while (stk.length && stk.at(-1) < heights[i]) {
148+
++ans[i];
149+
stk.pop();
150+
}
151+
if (stk.length) {
152+
++ans[i];
116153
}
117-
stack.push(i);
154+
stk.push(heights[i]);
118155
}
119156
return ans;
120157
}
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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-
while (!stk.empty()) {
9-
ans[i]++;
10-
if (heights[i] <= stk.top()) break;
11-
stk.pop();
12-
}
13-
stk.push(heights[i]);
14-
}
15-
return ans;
16-
}
17-
};
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; --i) {
8+
while (stk.size() && stk.top() < heights[i]) {
9+
++ans[i];
10+
stk.pop();
11+
}
12+
if (stk.size()) {
13+
++ans[i];
14+
}
15+
stk.push(heights[i]);
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func canSeePersonsCount(heights []int) []int {
2+
n := len(heights)
3+
ans := make([]int, n)
4+
stk := []int{}
5+
for i := n - 1; i >= 0; i-- {
6+
for len(stk) > 0 && stk[len(stk)-1] < heights[i] {
7+
ans[i]++
8+
stk = stk[:len(stk)-1]
9+
}
10+
if len(stk) > 0 {
11+
ans[i]++
12+
}
13+
stk = append(stk, heights[i])
14+
}
15+
return ans
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] canSeePersonsCount(int[] heights) {
3+
int n = heights.length;
4+
int[] ans = new int[n];
5+
Deque<Integer> stk = new ArrayDeque<>();
6+
for (int i = n - 1; i >= 0; --i) {
7+
while (!stk.isEmpty() && stk.peek() < heights[i]) {
8+
stk.pop();
9+
++ans[i];
10+
}
11+
if (!stk.isEmpty()) {
12+
++ans[i];
13+
}
14+
stk.push(heights[i]);
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
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
1+
class Solution:
2+
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
3+
n = len(heights)
4+
ans = [0] * n
5+
stk = []
6+
for i in range(n - 1, -1, -1):
7+
while stk and stk[-1] < heights[i]:
8+
ans[i] += 1
9+
stk.pop()
10+
if stk:
11+
ans[i] += 1
12+
stk.append(heights[i])
13+
return ans
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
function canSeePersonsCount(heights: number[]): number[] {
22
const n = heights.length;
3-
const ans = new Array(n).fill(0);
4-
const stack = [];
5-
for (let i = n - 1; i >= 0; i--) {
6-
while (stack.length !== 0) {
7-
ans[i]++;
8-
if (heights[i] <= heights[stack[stack.length - 1]]) {
9-
break;
10-
}
11-
stack.pop();
3+
const ans: number[] = new Array(n).fill(0);
4+
const stk: number[] = [];
5+
for (let i = n - 1; ~i; --i) {
6+
while (stk.length && stk.at(-1) < heights[i]) {
7+
++ans[i];
8+
stk.pop();
129
}
13-
stack.push(i);
10+
if (stk.length) {
11+
++ans[i];
12+
}
13+
stk.push(heights[i]);
1414
}
1515
return ans;
1616
}

0 commit comments

Comments
 (0)