643. Map of Highest Peak

MediumStackQueueBFSGrid

Given a grid isWater where 1 marks water and 0 land, assign each cell a height so that water cells are height 0, adjacent cells differ by at most 1, and the maximum height is as large as possible. Return any valid height matrix (the one produced by BFS from water). The input is JSON {isWater}.

Input: JSON {isWater}.

Output: Array — the height of each cell.

Examples

Example 1
Input: {"isWater":[[0,1],[0,0]]}
Output: [[1,0],[2,1]]
Explanation: Heights rise away from water.
Example 2
Input: {"isWater":[[0,0,1],[1,0,0],[0,0,0]]}
Output: [[1,1,0],[0,1,1],[1,2,2]]
Explanation: BFS distances from water.
Example 3
Input: {"isWater":[[1]]}
Output: [[0]]
Explanation: All water.

Constraints

Asked by

AmazonGoogleBloomberg
Solve this problem in the editor →