8. Trapping Rainwater

Given an array of non-negative integers, height representing the elevation of ground. Calculate the amount of water that can be trapped after rain.

Example 1:

Input: height= [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]

Output: 6

Explanation: As seen from the diagram 1+1+2+1+1=6 unit of water can be trapped

Example 2:

Input: height= [4, 2, 0, 3, 2, 5]

Output: 9

Expalanation: 2+4+1+2=9 unit of water can be trapped

Now Your Turn!

Pick the correct output for the given input

Input: height= [7, 4, 0, 9]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •   n == height.length
  •   1 <= n <= 105
  •   0 <= height[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution
{
public:
int trap(vector<int> &height){
}
};
Test Case

Input:

Height