42. Trapping Rain Water
Difficulty: Hard
Given _n_ non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
Example:
1 | **Input:** [0,1,0,2,1,0,1,3,2,1,2,1] |
Solution
Language: Java
累加得到柱子面积 14
先用一个递增队列,把上一个最高的柱子存起来
然后遍历的时候,算出柱子加水的总面积
1 1 2 2 2 2 3 2 2 2 1 = 20
所以水面积=20-14=6
1 | class Solution { |
第二种算总面积的方法,用一个left和right从两边开始扫描,left递增,right递增,每次算新一层的面积
1 | class Solution { |