Rainwater
InterviewTime limit1sMemory limit256 MB
Given stack heights across a 2D world, compute the total rainwater trapped between the blocks after heavy rain.
- Level
Medium5 of 10
- Topics
- Array, Two pointers, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Blocks are stacked in a two dimensional world. When it rains, rainwater collects between the blocks.


It rains more than enough. How much rainwater collects in total?
Input
The first line contains the height and the width of the two dimensional world. ()
The second line contains stack heights, given from the leftmost cell in order. Each height is an integer between and .
Blocks are piled up from the floor, so no empty space appears inside a stack. You may assume that the floor of the two dimensional world is always closed.
Output
One cell of the two dimensional world holds unit. Print the total amount of rainwater that collects.
If no rainwater collects at all, print .
Hint
Hint 1:

Hint 2:

Hint 3:
