Leetcode Problem 1765. Map of Highest Peak

1765. Map of Highest Peak

Leetcode Solutions

Multi-source Breadth-First Search (BFS)

  1. Initialize a queue to store the coordinates of water cells.
  2. Initialize a matrix to store the heights, with the same dimensions as isWater.
  3. Iterate over the isWater matrix to find all water cells and set their height to 0 in the height matrix and add them to the queue.
  4. Perform BFS: a. Dequeue a cell from the queue. b. Check its four adjacent cells (up, down, left, right). c. If an adjacent cell is land and has not been visited (height is not set), set its height to the current cell's height + 1. d. Add the adjacent cell to the queue.
  5. Continue until the queue is empty.
  6. Return the height matrix.
UML Thumbnail

Depth-First Search (DFS) with Height Increment

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...