42. Trapping Rain Water

 

Efficiently Solving the Trapping Rain Water Problem on LeetCode

Introduction

The Trapping Rain Water problem is a popular algorithmic challenge that can be found on LeetCode. It involves calculating how much water can be trapped after it rains, given an array representing the elevation map of a terrain. In this post, we'll walk through an efficient solution that utilizes the two-pointer approach to solve this problem in linear time.

Problem Statement

Given an array height representing the elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Intuition

To solve this problem, the key insight is to determine the amount of water that can be trapped above each bar. This can be achieved by calculating the maximum heights to the left and right of each bar. The amount of water trapped above a bar is determined by the minimum of these two heights, minus the height of the bar itself.

Approach

Our approach involves using two auxiliary arrays to store the maximum heights to the left and right of each bar, respectively. Here’s a step-by-step breakdown:

  1. Initialize Arrays:

    • We initialize two arrays, leftHigh and rightHigh, to keep track of the maximum heights to the left and right of each bar.

  2. Calculate Left High:

    • We traverse the array from left to right to fill the leftHigh array with the maximum height encountered so far.

  3. Calculate Right High:

    • We traverse the array from right to left to fill the rightHigh array with the maximum height encountered so far.

  4. Calculate Trapped Water:

    • We iterate through the elevation map, and for each bar, we compute the amount of water that can be trapped on top of it by taking the minimum of the maximum heights to the left and right of the bar and subtracting the height of the bar itself.

LeetCode Solution

Here's the complete JavaScript solution for the Trapping Rain Water problem:

javascript
var trap = function(height) {
    let n = height.length;
    let rightHigh = Array(n);
    let leftHigh = Array(n);
    // calculate the left High
    leftHigh[0] = height[0];
    for(let i = 1; i < n; i++) {
        leftHigh[i] = Math.max(height[i], leftHigh[i - 1]);
    }
    // calculate the right High
    rightHigh[n -1] = height[n - 1];
    for(let i = n - 2; i >= 0; i--) {
        rightHigh[i] = Math.max(height[i], rightHigh[i + 1]);
    }
    // calculate the result
    let result = 0;
    for(let i = 0; i < n; i++) {
        result += Math.min(leftHigh[i], rightHigh[i]) - height[i];
    }
    return result;
};

Complexity Analysis

Time Complexity: O(n)

  • Calculating the leftHigh array takes O(n) time.

  • Calculating the rightHigh array takes O(n) time.

  • Calculating the trapped water takes O(n) time.

  • Overall, the time complexity is linear, O(n).

Space Complexity: O(n)

  • The space required for the leftHigh and rightHigh arrays is O(n).

  • Overall, the space complexity is linear, O(n).

Conclusion

This solution efficiently solves the Trapping Rain Water problem by using auxiliary arrays to keep track of the maximum heights to the left and right of each bar and calculating the trapped water in linear time and space. This approach ensures that the solution is efficient even for large input sizes.

If you have any questions or suggestions, feel free to leave a comment. Happy coding!

For those interested in further details or alternative solutions to the Trapping Rain Water problem, you can check out this Trapping Rain Water - LeetCode which offers a simple and efficient approach with a time complexity of 

O(n)O(n).

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50