Find Each Island's Maximum Height
A rectangular grid uses 0 for water and a positive integer for each land cell's height. Land cells connect only through shared edges. Find the maximum height in every island.
Function Signature
island_max_heights(grid: list[list[int]]) -> list[int]
Valid Input Domain
The grid is rectangular and may be empty. Values are nonnegative integers; zero is water and any positive value is land.
Exact Output Semantics
Discover islands by scanning cells in row-major order. Return one maximum per island in that discovery order, where an island's discovery key is its first row-major land cell. This ordering makes the result canonical.
Constraints
-
0 <= rows, columns <= 1,000.
-
rows * columns <= 1,000,000.
-
1 <= land height <= 10^9.
Public Examples
Example 1
Input: grid = [[1, 0, 4], [2, 0, 3]]
Output: [2, 4]
The left island has maximum 2, and the right island has maximum 4; the left one is discovered first.
Example 2
Input: grid = [[0, 0], [0, 0]]
Output: []
There are no land cells and therefore no islands.
Hints
-
Extend the ordinary island traversal with a running maximum for the current component.
-
The outer row-major scan determines output order; neighbor visitation order does not.