Trapping Rain Water
Last updated
Last updated
Array, Two Pointers
Given n
non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
Example 1:
Example 2:
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
Assumptions:
There is no number calculation overflow.
There is no any negative value element.
Questions to Clarify:
What would be the size of the input array?
The problem is about calculating the total amount of rainwater that can be trapped between bars of different heights. The solution involves iterating over each bar and calculating the amount of water that can be trapped on top of it.
For each bar, we find the maximum height of the bars to its left and right. The water that can be trapped on top of the current bar is the minimum of these two maximum heights minus the height of the current bar. This is because the water level cannot be higher than the shorter bar around it, and the height of the bar itself occupies space where water could have been trapped.
This algorithm calculates the water trapped at each index by finding the maximum height of bars on the left and right side. The water trapped at a particular index is the minimum of these two maximum heights minus the height at that index.
Here, the area calculation formula for this problem is:
maxWater += min(maxLeft,maxRight) - height[current]
Steps
Initialize totalWater to 0: This variable will keep track of the total amount of water that can be trapped.
Iterate over each bar (from 0 to length of the array): For each bar, we calculate the amount of water that can be trapped on top of it.
Find the maximum height of the bars on the left of the current bar: We initialize leftMax
to 0 and iterate from the current bar to the first bar of the array, updating leftMax
with the maximum height encountered.
Find the maximum height of the bars on the right of the current bar: Similarly, we initialize rightMax
to 0 and iterate from the current bar to the last bar of the array, updating rightMax
with the maximum height encountered.
Calculate the water trapped on the current bar: The amount of water that can be trapped on the current bar is the minimum of leftMax
and rightMax
minus the height of the current bar. This is because the water level cannot be higher than the shorter bar around it, and the height of the bar itself occupies space where water could have been trapped.
Add the water trapped on the current bar to totalWater: We add the amount of water trapped on the current bar to totalWater
.
Return totalWater: After the loop finishes, totalWater
will be the total amount of water that can be trapped according to the input array.
Complexity
Time complexity: O(N^2)
Space complexity: O(1)
The problem is about calculating the total amount of rainwater that can be trapped between bars of different heights. The solution involves using two pointers, one starting from the beginning of the array (left) and the other from the end of the array (right).
The algorithm maintains two variables, maxLeft
and maxRight
, to keep track of the maximum height of the bars seen so far from the left and right, respectively.
In each step, the algorithm compares maxLeft
and maxRight
. If maxLeft
is less than maxRight
, it means the water trapped on the bar at the left
index would be maxLeft - height[left]
, because maxLeft
is the minimum of the two. So, it adds this to totalWater
and moves the left
pointer one step to the right.
If maxLeft
is not less than maxRight
, it does the same operation with the right
pointer and maxRight
.
The algorithm continues this process until the left
and right
pointers meet. At the end, totalWater
will be the total amount of water that can be trapped according to the input array.
Steps
Check if the array length is less than or equal to 2: If the array length is less than or equal to 2, there can't be any trapped water, so return 0.
Initialize variables: n
is the length of the array. maxLeft
and maxRight
are the maximum heights of the bars on the left and right ends of the array, respectively. left
and right
are pointers to the second bar from the left and the second bar from the right, respectively. totalWater
is initialized to 0 to keep track of the total amount of trapped water.
Start a while loop that continues as long as left
is less than or equal to right
: This loop traverses the array from both ends towards the center.
Check if maxLeft
is less than maxRight
: If maxLeft
is less than maxRight
, then the water trapped on the current bar depends on maxLeft
.
If the height of the bar at the left
index is greater than maxLeft
, update maxLeft
to this height.
Otherwise, add maxLeft - height[left]
to totalWater
(this is the amount of water that the current bar can trap), and increment left
by 1.
If maxLeft
is not less than maxRight
, then the water trapped on the current bar depends on maxRight
.
If the height of the bar at the right
index is greater than maxRight
, update maxRight
to this height.
Otherwise, add maxRight - height[right]
to totalWater
(this is the amount of water that the current bar can trap), and decrement right
by 1.
Return totalWater
: After the loop finishes, totalWater
will be the total amount of water that can be trapped according to the input array.
Complexity
Time complexity: O(N)
Space complexity: O(1)