Trapping Rain Water II
The problem requires finding the volume of water that can be trapped on a 2D elevation map. We can approach it by using a Priority Queue.


This code will calculate the trapped rainwater volume based on the given heightMap. It starts by adding the boundary cells to a priority queue and then iteratively processes cells from the queue, updating the water volume based on the heights of the neighboring cells. It ensures that the processed cells are marked as visited to avoid double counting.
Identifying Problem Isomorphism
“Trapping Rain Water II” is approximately isomorphic to “Swim in Rising Water”.
In “Trapping Rain Water II”, you have a 2D grid representing an elevation map where the value at each point is the height above sea level. Rain starts to fall, and you need to calculate how much rainwater can be trapped in the grid.
In “Swim in Rising Water”, you also have a 2D grid representing an elevation map. The water starts to rise from level 0, and you need to find the minimum time to swim to the other end of the grid.
Both involve a 2D grid and an incrementally increasing water level, and the tasks relate to how the water interacts with the landscape’s elevation. The key difference is in the tasks: in “Trapping Rain Water II”, you calculate the trapped rainwater, while in “Swim in Rising Water”, you calculate the minimum time to swim to the other end of the grid.
“Swim in Rising Water” is simpler because it requires finding a path through the grid with a monotonic function (minimum time), while “Trapping Rain Water II” requires calculating the volumetric capacity of the landscape, which is more complex.
10 Prerequisite LeetCode Problems
“Trapping Rain Water II” requires understanding of data structures such as priority queues, and the ability to visualize and traverse in three dimensions. Here are 10 problems to prepare:
“Trapping Rain Water” (LeetCode Problem #42): This problem is a simpler version that only involves trapping rain water in one dimension.
“Binary Heap Operations” (LeetCode Problem #1405): This problem gives a basic understanding of how a priority queue (heap) operates, which is necessary for the “Trapping Rain Water II” problem.
“Heap Implementation” (LeetCode Problem #1439): This is another heap related problem which helps in understanding priority queues.
“Kth Largest Element in an Array” (LeetCode Problem #215): This problem requires the use of a priority queue to solve it and provides practice on the application of this data structure.
“Top K Frequent Elements” (LeetCode Problem #347): This is a bit more complex heap related problem and provides a nice practice to understand priority queues.
“Merge k Sorted Lists” (LeetCode Problem #23): This problem also requires the use of a priority queue and helps you understand how to use it in a more complex scenario.
“Flood Fill” (LeetCode Problem #733): This problem helps you to understand the concept of flooding an area, which is applicable in the “Trapping Rain Water II” problem.
“01 Matrix” (LeetCode Problem #542): This problem requires multidirectional traversal, much like “Trapping Rain Water II”.
“Rotting Oranges” (LeetCode Problem #994): This problem gives you practice on gridbased breadthfirst search (BFS), which will be useful in “Trapping Rain Water II”.
“Walls and Gates” (LeetCode Problem #286): This problem involves updating distances in a grid, which can be helpful to understand the multidirectional traversal required in “Trapping Rain Water II”.


Problem Classification
Trapping Rain Water II  The problem asks to compute the volume of water trapped on a 3D terrain. This is a Volume Calculation Problem.
Language Agnostic Coding Drills
Array Manipulation: Learn how to create, access, and manipulate arrays. This is an important skill since the
heightMap
variable is a 2dimensional array.Conditional Statements: Master the use of
if
statements to control the flow of a program. Here, it’s used to check for edge cases and control program flow.Loops: Understand how to use
for
loops to iterate over data structures. This code includes a nestedfor
loop to iterate over the elements inheightMap
.Heap Data Structure: Familiarize yourself with heap data structures and the Python
heapq
module. In this code, the heap is used to store the boundary elements ofheightMap
and to maintain a minheap data structure for efficient retrieval of the smallest element.Tuple Data Structure: Learn how to create and manipulate tuples. Here, each element in the heap is a tuple of (height, x coordinate, y coordinate).
2D Array Edge Cases: Understand the edge cases when working with 2dimensional arrays. This problem involves the boundaries of the
heightMap
, hence, it’s important to understand how to check if a given coordinate (i, j) is within the boundaries of theheightMap
.
The problemsolving approach for this problem can be described as follows:
Handle edge cases: If the
heightMap
is empty or has less than 3 rows or columns, it cannot trap any rainwater, so return 0.Initialize a heap with all the boundary cells of
heightMap
.Pop elements (cells) from the heap in increasing order of their height. For each cell, check its neighbours. If a neighbour cell’s height is less than the current water level, it can trap rainwater and add the volume to the result.
Mark visited cells to avoid revisiting them.
Push unvisited neighbour cells into the heap.
Repeat steps 35 until the heap is empty. Return the total volume of trapped rainwater.
This solution uses a priority queue (heap) to perform a breadthfirst search (BFS) from the outside inwards, always processing the cells with the lowest height first.
Targeted Drills in Python
Here are coding drills for each concept involved in the solution. Note that these are the building blocks for the final problem’s solution.
1. Array Manipulation
In Python, you can manipulate lists, which are equivalent to arrays in other languages.


2. Conditional Statements
Python supports the usual logical conditions known from mathematics. These can be used in several ways, most commonly in “if statements”.


3. Loops
In Python, for
and while
loops are commonly used.


4. Heap Data Structure
Python provides a module named heapq
for efficient implementation of heap data structures.


5. Tuple Data Structure
A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.


6. 2D Array Edge Cases
When dealing with 2dimensional arrays, it’s important to make sure any indices used are valid.


By understanding and practicing these smaller concepts, you can build up to understanding and solving the final problem of trapping rainwater.