Oracle OA 面试真题解析:Max Altitude of Islands|岛屿最大高度

52次阅读
没有评论

Max Altitude of Islands

Given an m x n 2D grid which represents a map where positive integers represent elevation of land and 0‘s represent water, return the altitude of the highest point for each of the islands.

An island is surrounded by water and is formed by connecting adjacent lands either horizontally or vertically.

You may assume all four edges of the grid are surrounded by water.

Input:

map = [[1,1,1,1,0],
  [1,8,0,10,0],
  [1,1,0,0,0],
  [0,0,0,0,0]
]

Output: 1 island with highest point is 10

这道题给定一个二维网格,数字大于 0 表示陆地高度,0 表示海水,需要找出每一座岛屿中的最高海拔。岛屿由上下左右相邻的陆地连通形成,因此本质上是一个“连通块 + 取最大值”的问题。通常可以用 DFS 或 BFS 遍历每个未访问的陆地格子,在遍历同一座岛屿时不断更新最大高度,最后得到该岛屿的峰值。示例中左侧岛屿包含 1、8 等格子,最高点为 8;右侧独立陆地 10 形成另一座岛屿,答案就是对应岛屿的最高点。

正文完
 0