Measure Water Trapped by an Elevation Grid

Quick Overview

Practice measuring water retained by a rectangular elevation map with drainage at its boundary. Account for connected escape routes, thin grids, and total volume.

Measure Water Trapped by an Elevation Grid

Company: Media.net

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Measure Water Trapped by an Elevation Grid An `m` by `n` matrix `heightMap` gives the height of each unit-area cell in an elevation map. After enough rain, water can drain from the outer boundary of the map. Return the total volume retained above its cells. Water can flow between cells sharing an edge; diagonal contact alone does not provide a passage. Each unit of water depth over one cell contributes one unit of volume. ### Function `int trapRainWater(vector<vector<int>>& heightMap)` ### Example 1 ```text Input: heightMap = [[4,4,4,4],[4,1,2,4],[4,4,4,4]] Output: 5 ``` The two interior cells retain depths `3` and `2`. Boundary cells retain no water. ### Example 2 ```text Input: heightMap = [[5,5,5],[5,1,2],[5,5,5]] Output: 1 ``` The boundary cell of height `2` lets water escape. The center cell can retain only one unit of depth. ### Constraints - `m == heightMap.length` and `n == heightMap[i].length`. - `1 <= m,n <= 200`. - `0 <= heightMap[i][j] <= 20000`. ### Clarifications The matrix is rectangular. Count water volume, not the heights of the ground cells. A low route to the outer boundary can limit retention far from the edge; the immediate neighbors alone do not always determine a cell's final depth. ### What a Strong Answer Covers - Drainage through the outer boundary and low escape routes. - Multiple basins, flat ground, and grids without interior cells. - Why the calculated depths are simultaneously attainable and cannot be exceeded. - Correctness, numeric safety, and resource bounds.

Overview: Practice measuring water retained by a rectangular elevation map with drainage at its boundary. Account for connected escape routes, thin grids, and total volume.

Media.net logo
Media.net
hardSoftware EngineerOtherCoding & Algorithms
0
0

Measure Water Trapped by an Elevation Grid

An m by n matrix heightMap gives the height of each unit-area cell in an elevation map. After enough rain, water can drain from the outer boundary of the map. Return the total volume retained above its cells.

Water can flow between cells sharing an edge; diagonal contact alone does not provide a passage. Each unit of water depth over one cell contributes one unit of volume.

Function

int trapRainWater(vector<vector<int>>& heightMap)

Example 1

Input: heightMap = [[4,4,4,4],[4,1,2,4],[4,4,4,4]]
Output: 5

The two interior cells retain depths 3 and 2. Boundary cells retain no water.

Example 2

Input: heightMap = [[5,5,5],[5,1,2],[5,5,5]]
Output: 1

The boundary cell of height 2 lets water escape. The center cell can retain only one unit of depth.

Constraints

  • m == heightMap.length and n == heightMap[i].length .
  • 1 <= m,n <= 200 .
  • 0 <= heightMap[i][j] <= 20000 .

Clarifications

The matrix is rectangular. Count water volume, not the heights of the ground cells. A low route to the outer boundary can limit retention far from the edge; the immediate neighbors alone do not always determine a cell's final depth.

What a Strong Answer Covers Guidance

  • Drainage through the outer boundary and low escape routes.
  • Multiple basins, flat ground, and grids without interior cells.
  • Why the calculated depths are simultaneously attainable and cannot be exceeded.
  • Correctness, numeric safety, and resource bounds.