FastPrepTrapping Rain Water II

Trapping Rain Water II

Bloomberg LP logoBloomberg LP● HardNEW GRADPHONE SCREEN
Learn

Problem statement

You are given an m x n integer matrix heightMap, where each value is the height of one unit-width cell in a two-dimensional terrain.

After rain falls, water can remain above a cell only when surrounding terrain prevents it from flowing to the outer boundary.

Return the total volume of trapped water.

Function

trapRainWater(heightMap: int[][]) → int

Examples

Example 1

heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]return = 4

The boundary-driven water level traps four units across the two interior low regions.

Example 2

heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]return = 10

The boundary height is three. The eight height-two cells hold one unit each, and the center holds two.

Example 3

heightMap = [[1,1,1],[1,0,1],[1,1,1]]return = 1

The center cell is enclosed by height-one boundary cells.

Constraints

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

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int trapRainWater(int[][] heightMap) {
    // Write your code here.
}
heightMap[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
expected4
Checking account…