2Brute2Best
🔥0

Map of Highest Peak

Medium·LeetCode #1765 Pro OnlyMedium
LeetCode
01

Problem Statement

You get a map where each cell is either water or land. Assign every cell a height so that:

  • every water cell has height 0,
  • every height is 0 or more,
  • any two cells that share a side differ in height by at most 1.

Choose the heights so that the highest cell on the map is as high as possible. Return the height grid. If several grids reach the largest possible top height, any of them is accepted.

Input

isWater, an m × n grid. 1 = water, 0 = land. There is at least one water cell.

Output

An m × n height grid that follows the rules above and whose highest cell is as high as possible.

Example 1
Input: isWater = [[0,1],[0,0]]
Output: [[1,0],[2,1]]

The water is the top-right cell, at height 0. The land next to it is at height 1, and the far corner is 2 steps from the water, so it can be 2.

Example 2
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]]
Output: [[1,1,0],[0,1,1],[1,2,2]]

The highest cell can reach 2. The bottom-middle cell is 2 steps from the nearest water, so 2 is the most it can be.

Example 3
Input: isWater = [[1,0,0,0]]
Output: [[0,1,2,3]]

The only water is at the left end. The height goes up by 1 for each step away from it.

Constraints
  • • m == isWater.length and n == isWater[i].length
  • • 1 ≤ m, n ≤ 1000
  • • Each cell is 0 (land) or 1 (water).
  • • There is at least one water cell.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“a neighbour differs by at most 1”
→ Each step away from water can raise a cell by at most one, so the height is a distance from water.
“water cells are 0”
→ Every water cell is a source at distance 0. Many sources at once means multi-source BFS.
“the highest possible peak”
→ Make every land cell as high as its distance allows, which is its distance from the nearest water.
03

Approach Complexity Ladder

Brute Force — sweep and lower until nothing changesO((R · C)²)O(R · C)

Start with water at 0 and land at unknown; sweep the grid, lowering each land cell to a neighbour's height + 1, until a sweep changes nothing

Optimal — multi-source BFS from all waterO(R · C)O(R · C)

Put every water cell in the queue at height 0; each land cell gets the height of the first wave that reaches it

Brute Force Intuition

Start water at 0 and land at 'unknown'. Sweep the grid. For each land cell, look at its four neighbours and lower the cell to one more than the smallest neighbour if that is lower than its current value. Repeat sweeps until a whole sweep changes nothing. The stable grid is the answer.

Recognition rule for this sub-pattern:

Many starting points spread at the same time — push all of them into the queue first.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.