Trapping Rain Water LeetCode Solution

Problem – Trapping Rain Water

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:

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.

Example 2:

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


  • n == height.length
  • 1 <= n <= 2 * 104
  • 0 <= height[i] <= 105

Trapping Rain Water LeetCode Solution in C++

class Solution {
    int trap(int A[], int n) {
        int left=0; int right=n-1;
        int res=0;
        int maxleft=0, maxright=0;
                if(A[left]>=maxleft) maxleft=A[left];
                else res+=maxleft-A[left];
                if(A[right]>=maxright) maxright= A[right];
                else res+=maxright-A[right];
        return res;

Trapping Rain Water LeetCode Solution in Python

class Solution(object):
    def trap(self, height):
        :type height: List[int]
        :rtype: int
        leftCursor, rightCursor = 0, len(height)-1
        leftMax, rightMax, storedWater = 0, 0, 0
        while (leftCursor <= rightCursor):
            leftMax = max(leftMax, height[leftCursor])
            rightMax = max(rightMax, height[rightCursor])
            if leftMax < rightMax:
                storedWater += leftMax - height[leftCursor]
                leftCursor += 1
                storedWater += rightMax - height[rightCursor]
                rightCursor -= 1
        return storedWater

Trapping Rain Water LeetCode Solution in Java

class Solution {
    public int trap(int[] height) {
        if (height == null || height.length == 0) {
            return 0;
        int left = 0; int right = height.length - 1; // Pointers to both ends of the array.
        int maxLeft = 0; int maxRight = 0;
        int totalWater = 0;
        while (left < right) {
            // Water could, potentially, fill everything from left to right, if there is nothing in between.
            if (height[left] < height[right]) {
                // If the current elevation is greater than the previous maximum, water cannot occupy that point at all.
                // However, we do know that everything from maxLeft to the current index, has been optimally filled, as we've
                // been adding water to the brim of the last maxLeft.
                if (height[left] >= maxLeft) { 
                    // So, we say we've found a new maximum, and look to see how much water we can fill from this point on.
                    maxLeft = height[left]; 
                // If we've yet to find a maximum, we know that we can fill the current point with water up to the previous
                // maximum, as any more will overflow it. We also subtract the current height, as that is the elevation the
                // ground will be at.
                } else { 
                    totalWater += maxLeft - height[left]; 
                // Increment left, we'll now look at the next point.
            // If the height at the left is NOT greater than height at the right, we cannot fill from left to right without over-
            // flowing; however, we do know that we could potentially fill from right to left, if there is nothing in between.
            } else {
                // Similarly to above, we see that we've found a height greater than the max, and cannot fill it whatsoever, but
                // everything before is optimally filled
                if (height[right] >= maxRight) { 
                    // We can say we've found a new maximum and move on.  
                    maxRight = height[right]; 
                // If we haven't found a greater elevation, we can fill the current elevation with maxRight - height[right]
                // water.
                } else {
                    totalWater += maxRight - height[right]; 
                // Decrement left, we'll look at the next point.
        // Return the sum we've been adding to.
        return totalWater;
Trapping Rain Water LeetCode Solution Review:

In our experience, we suggest you solve this Trapping Rain Water LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Trapping Rain Water LeetCode Solution

Find on LeetCode


I hope this Trapping Rain Water LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *