15. Map of Highest Peak
Last updated
Last updated
You are given an integer matrix isWater
of size m x n
that represents a map of land and water cells.
If isWater[i][j] == 0
, cell (i, j)
is a land cell.
If isWater[i][j] == 1
, cell (i, j)
is a water cell.
You must assign each cell a height in a way that follows these rules:
The height of each cell must be non-negative.
If the cell is a water cell, its height must be 0
.
Any two adjacent cells must have an absolute height difference of at most 1
. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).
Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix height
of size m x n
where height[i][j]
is cell (i, j)
's height. If there are multiple solutions, return any of them.
Example 1:
Example 2:
Approach: As max abs difference is 1 Extract all water and use bfs to add values in adjacent cells
Time and Space Complexity: O(n * m)