bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 407. Trapping Rain Water II

407. Trapping Rain Water II

Leetcode Solutions

Priority Queue based BFS

  1. Initialize a priority queue (min-heap) to store cell information (height, row, column).
  2. Add all boundary cells to the priority queue and mark them as visited.
  3. Initialize a result variable to 0 to store the total volume of trapped water.
  4. While the priority queue is not empty: a. Pop the cell with the lowest height from the priority queue. b. Check all unvisited neighbors of the current cell. c. If a neighbor can trap water (its height is less than the current cell's height), add the trapped water volume to the result. d. Add the neighbor to the priority queue with a height equal to the maximum of its own height and the current cell's height. e. Mark the neighbor as visited.
  5. Return the result.
UML Thumbnail

DFS with Elevation Adjustment

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...