Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Number of Visible People in a Queue
Bookmark
Brute Force
Monotonic Stack
Input
Example 1
Example 2
Example 3
Custom
heights
=
[10, 6, 8, 5, 11, 9]
queue
10
6
8
5
11
9
0
1
2
3
4
5
result
how many people to the right can each person see?
queue
10
6
8
5
11
9
0
1
2
3
4
5
result
how many people to the right can each person see?
visible =
1
queue
10
6
j
8
5
11
9
0
1
2
3
4
5
i
result
6
> wall 0
sees person 1
visible =
2
tallest between = 8
queue
10
6
8
5
j
11
9
0
1
2
3
4
5
i
result
5
hidden behind wall 8
visible =
0
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
person 1, height
6
visible =
1
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
1
result[1] =
1
visible =
2
tallest between = 5
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
1
11
>= 8, visible then view blocked. count = 2
visible =
0
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
1
2
person 3, height
5
visible =
0
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
1
2
1
person 4, height
11
visible =
1
tallest between = 9
queue
10
6
8
5
11
9
0
1
2
3
4
5
i
result
3
1
2
1
1
result[4] =
1
queue
10
6
8
5
11
9
0
1
2
3
4
5
result
3
1
2
1
1
0
return
[3, 1, 2, 1, 1, 0]
algo
master
.
io
Step:
Brute force: for each person, walk right and count who they can see.
0 / 23
Input
Example 1
Example 2
Example 3
Custom
heights
=
[10, 6, 8, 5, 11, 9]
0 / 23
algo
master
.
io
Step:
Brute force: for each person, walk right and count who they can see.