Trapping Rain Water

problem [https://leetcode.com/problems/trapping-rain-water/ 42. Trapping Rain Water].

Problem description
Given  non-negative integers representing an elevation map where the width of each bar is , compute how much water it can trap after raining.


 * Example 1
 * Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
 * Output: 6
 * Explanation: The above elevation map (black section) 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.
 * Trapping Rain Water.png


 * Example 2
 * Input: height = [4,2,0,3,2,5]
 * Output: 9


 * Constraints

Problem analysis
This problem can be solved using two pointers, by starting from both ends of the input data, and moving the pointers step by step, until the  and   pointers meet together.

So how do we know that where we can trap water, and how much water can be trapped at each position?

We are able to see that the input data may contain peaks and valleys, and obviously rain water can only be trapped inside valleys.
 * A valley is a subarray  in the input data such that, for every position i in the subarray, the conditions   and   are both satisfied.
 * A peak is a subarray  in the input data such that every element has the same height h, and the conditions   and   are both satisfied.
 * Note that a peak may be part of a valley. But this is irrelevant to problem solving, since there will be water filling up above this peak, and the water height is constrained by the lower height at either end of the valley.

Now for any interval, there may be three cases in terms of the number of valleys:
 * 1) No valley. The interval may be flat or monotonically increasing / decreasing, so it cannot trap any rain water at all.
 * 2) One valley. All positions between l and r (not including l and r) will trap rain water.
 * 3) Two or more valleys. The interval can be further divided into sub-intervals such that each interval contains one valley.

For each valley, at each single position i (excluding both ends) the water amount is  , where lh and rh are the constraining heights on left and right respectively, i.e.   and.

So the overall procedure to compute the total amount of rain water is as follows:
 * 1) Initialize ,  ,  ,  ,.
 * 2) While  :
 * 3) Always pick the pointer that is pointing to a lower height. Or if , we choose to move the left pointer (it doesn't matter to move either one in this case). If picked  , then move   to the right by 1 step. Otherwise, move   to the left by 1 step.
 * 4) After moving the pointer:
 * 5) * If moved the  pointer:
 * 6) ** If, then the position will capture   units of water. Add the amount to.
 * 7) ** Otherwise the position  cannot be used to trap water, but may become a constraining height instead in the future. So set.
 * 8) * Similarly, if moved the  pointer:
 * 9) ** If, then the position will capture   units of water. Add the amount to.
 * 10) ** Otherwise the position  cannot be used to trap water, but may become a constraining height instead in the future. So set.
 * 11) After the two pointers meet, i.e. , return.



Complexity analysis

 * Time complexity
 * Since we move the pointers  and   until the two pointer meet, and within each iteration there is O(1) amount of computation, the total time complexity is O(n), where n is the number of inputs.


 * Space complexity
 * Since we did not allocate additional data structures, the space complexity is O(1).