Description
You are given an array of integers that represent the height of apartment buildings.
The leftmost building is at index 0, and the array lists the buildings from left to right. After the rightmost building, there is the ocean.
Buildings are of the same width and arranged in a straight line from left to right toward the ocean.
Return the indexes of the buildings that have an ocean view.
Example:
Input: [4, 3, 2, 3, 1]
Output: [0, 3, 4]
You are also given a game board represented as a 2D array of zeroes and ones.
0 stands for passable positions, and 1 stands for impassable positions.
Design an algorithm to find a path from the top-left corner to the bottom-right corner.
For example, for the following board:
entrance ->
0 0 0 0 0 0 0
0 0 1 0 0 1 0
0 0 1 0 1 1 0
0 0 1 0 1 0 1
1 1 1 0 0 0 0 -> exit
A possible path is:
entrance ->
+ + + + 0 0 0
0 0 1 + 0 1 0
0 0 1 + 1 1 0
0 0 1 + 1 0 1
1 1 1 + + + + -> exit
Assuming a zero-indexed grid of rows and columns, with (0, 0) at the top-left corner (entrance).
这题其实包含两个经典基础题:第一个是“海景房”问题,思路是从右往左扫描,维护当前见过的最高楼;只有当某栋楼高度严格大于右侧最高值时,它才可以看到海,因此可以在 O(n) 时间内收集所有下标。第二个是网格路径查找问题,给定 0/1 矩阵,0 表示可走、1 表示障碍,需要判断是否存在从左上角到右下角的路径,常见做法是用 DFS 或 BFS 在四个方向上搜索,并用 visited 防止重复访问。两题都很适合考察数组遍历、边界处理和基础搜索能力。