Given

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.
Output: 9
Constraints:
n == height.length 1 <= n <= 2 * 104 0 <= height[i] <= 105
Contents
Solution 1 - Using temporary arrays to store maximum heights on both side, and use minimum of maximum heights to calculate how mush water can be trapped Solution 2 - With the intution from solution 1, solve it using two pointers
The main intution behind this solution is, if you want to trap water between two heights, you can only trap upto the minimum height between those two heights.
So, in this approach we are going find maximum heights on both sides for the current height, then calculate the water that can be trapped at that height as
In this approach, we will use a temporary array and copy last
Implementation steps:
-
First, we will create two temporary arrays with same size as
height ,leftMax to store maximum heights on left side to current element, andrightMax to store maximum heights on right side to current element. -
Then, run a
for loop onheight array from index 1 to n-1 and at every indexi store max element seen so far on left side and store them inleftMax . -
Next, run another
for loop onheight array from index n-2 to 0 and at every indexi store max element seen so far on right side and store them inrightMax . -
Now, run another
for loop onheight array, and at each index calculate the water that can be trapped as:result = Math.max(0, Math.min(leftMax[i], rightMax[i]) - height[i]);
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the temporary arrays.
In this approach, we will use the intution from Solution 1 above, and notice that we are looking at minimum of two maximum elements on left and right side. So, in this approach we will use two pointers to hold maximum elements on left side and right side at the point (not necessarily the true maximum value, since we only care about minimum of those two) and apply the solution as above.
Implementation steps:
-
Let's declare 5 variables:
-
l : left pointer for the array, initialize with 0. -
r : right pointer for the array, initialize with height.length - 1. -
leftMax : maximum element seen so far on left side, initialize with height[l]. -
rightMax : maximum element seen so far on right side, initialize with height[r].
-
-
Next, run a
while loop, until l and r each other, and check the following:-
If
leftMax < rightMax : Increment the left pointerl , and calculate how much water can be trapped, then update theleftMax for the next element. -
If
leftMax >= rightMax : Decrement the right pointerr , and calculate how much water can be trapped, then update therightMax for the next element.
Incase both left and right heights are same, we can either shift left pointer l , or right pointerr , since we only need minimum of those two. -
If
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1).
Above implementations source code can be found at