Terrain Rainwater Volume Problem

Terrain Rainwater Volume Problem — ExecCode Hard DSA Practice

Solve the Terrain Rainwater Volume problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Rain falls on a height map. Water trapped in a cell cannot leak past a higher surrounding ridge; it can flow out through the lowest boundary. The 1D trapping-rain-water trick is not enough: water path-finds in 2D. Return how many units of water the terrain can trap after it rains. You are given: height_map — m×n non-negative heights Return non-negative integer volume. Cells on the border cannot trap water. A completely flat map traps nothing.

Examples

Input {'heightmap': [[1, 4, 3, 1, 3, 2], [3, 2, 1, 3, 2, 4], [2, 3, 3, 2, 3, 1]]}; Output 4. Input {'heightmap': [[3, 3, 3], [3, 1, 3], [3, 3, 3]]}; Output 2. Input {'height_map': [[2, 2], [2, 2]]}; Output 0

Constraints

Water flows 4-directionally, not diagonally

Practice Terrain Rainwater Volume free on ExecCode. Browse DSA problems, topic map, and placement guides.