Problem:
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.
Introduction: Rainwater trapping is a common problem in algorithmic interviews and coding challenges. In this blog post, we will explore an efficient solution to the rainwater trapping problem using a two-pointer approach. We will walk through the provided JavaScript code and explain how it works step by step. So let's dive in!
Example:
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.
Code :
/**
* @param {number[]} height
* @return {number}
*/
var trap = function(height) {
let i = 0, j = height.length - 1;
let leftMax = height[i];
let rightMax = height[j];
let result = 0;
if (!height) return 0;
while (i < j) {
if (leftMax < rightMax) {
i++;
leftMax = Math.max(leftMax, height[i]);
result += leftMax - height[i];
} else {
j--;
rightMax = Math.max(rightMax, height[j]);
result += rightMax - height[j];
}
}
return result;
};
Explanation: The above code provides an efficient solution to the problem of trapping rainwater using a two-pointer approach. Let's break it down step by step.
Initialize two pointers,
i
andj
, at the beginning and end of theheight
array, respectively. Also, initialize variablesleftMax
andrightMax
to store the maximum heights encountered on the left and right sides, respectively.Set the
result
variable to 0, which will accumulate the trapped rainwater.Check if the
height
array is empty. If it is, return 0 since there is no rainwater to trap.Enter a while loop that continues until the
i
pointer crosses thej
pointer. This loop efficiently traverses the array from both ends towards the center.Inside the loop, compare the values of
leftMax
andrightMax
to determine which side has a lower maximum height.If
leftMax
is smaller thanrightMax
, increment thei
pointer, updateleftMax
if a higher height is encountered, and calculate the difference betweenleftMax
and the current height ati
. Add this difference to theresult
variable.If
rightMax
is smaller than or equal toleftMax
, decrement thej
pointer, updaterightMax
if a higher height is encountered, and calculate the difference betweenrightMax
and the current height atj
. Add this difference to theresult
variable.After the loop, the
result
variable will contain the total trapped rainwater. Return this value.
Conclusion: In this blog post, we explored an efficient solution to the rainwater trapping problem using a two-pointer approach. By leveraging the two pointers and comparing the maximum heights encountered from both ends, we were able to calculate the trapped rainwater in linear time. The above JavaScript code demonstrates this approach and can be used as a reliable solution for rainwater trapping scenarios.
Happy coding!
Note: Feel free to modify the code as needed and provide appropriate context and examples to make the blog more comprehensive and informative.