Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Buildings With an Ocean View
Bookmark
Brute Force
Right-to-Left
Monotonic Stack
Input
Input 1
Input 2
Input 3
Custom
heights
=
[4, 2, 3, 1]
skyline
ocean
4
0
2
1
3
2
1
3
skyline
ocean
4
0
2
1
3
2
1
3
skyline
ocean
4
0
2
1
3
2
1
3
i
building
4
: is everything to its right shorter?
skyline
ocean
4
0
2
1
3
2
1
3
i
j
2
< 4 → doesn't block, keep checking
skyline
ocean
4
0
2
1
3
2
1
3
i
j
1
< 4 → doesn't block, keep checking
skyline
ocean
4
0
2
1
3
2
1
3
i
j
ocean view!
nothing taller to the right
skyline
ocean
4
0
2
1
3
2
1
3
i
j
3 >= 2
→ blocked, no ocean view
skyline
ocean
4
0
2
1
3
2
1
3
i
building
3
: is everything to its right shorter?
skyline
ocean
4
0
2
1
3
2
1
3
i
j
ocean view!
nothing taller to the right
skyline
ocean
4
0
2
1
3
2
1
3
i
building
1
: is everything to its right shorter?
skyline
ocean
4
0
2
1
3
2
1
3
ocean view =
[0, 2, 3]
algo
master
.
io
Step:
Brute force: a building has a view if every building to its right is shorter.
0 / 14
Input
Input 1
Input 2
Input 3
Custom
heights
=
[4, 2, 3, 1]
0 / 14
algo
master
.
io
Step:
Brute force: a building has a view if every building to its right is shorter.