Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Trapping Rain Water problem solution

YASH PAL, 31 July 2024

In this Leetcode Trapping Rain Water problem solution, we have 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.

Leetcode Trapping Rain Water problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

class Solution:
    def trap(self, height: List[int]) -> int:
        if not height: return 0
        
        water, n = 0, len(height)
        max_left, max_right = [None] * n, [None] * n
        max_left[0], max_right[-1] = height[0], height[-1]
        
        for i in range(1, n):
            max_left[i] = max(max_left[i-1], height[i])
            
        for i in range(n-2, 0, -1):
            max_right[i] = max(max_right[i+1], height[i])
            water += min(max_left[i], max_right[i]) - height[i]
            
        return water

Problem solution in Java.

class Solution {
    public int trap(int[] height) {
        int l=0;
        int r=height.length-1;
        while(l<r && height[l]<height[l+1])
            l++;
        while(l<r && height[r]<height[r-1])
            r--;
        int ans=0;
        
        while(l<r){
            int left=height[l];
            int right=height[r];
         if(left<=right){   
            while(l<r && left>=height[l]){
                ans+=left-height[l];
                l++;
            }}
        else{
            while(l<r && right>=height[r]){
                ans+=right-height[r];
                r--;
            }}
        }
        return ans;
    }
}

Problem solution in C++.

int trap(vector<int>& height) {
                
        if (height.size()==0) return 0;

        
        vector<int> heightcopy;
        
        heightcopy=height;
        
        int i,j;
        int Level;
        
        i=0;
        j=height.size()-1;
        Level=0;
        
        while (i<j) {
            if (heightcopy[i]<Level) { heightcopy[i]=Level; i++; }
            else if (heightcopy[i]==Level) i++;
            else if (heightcopy[j]<Level) { heightcopy[j]=Level; j--; }
            else if (heightcopy[j]==Level) j--;
            else if (heightcopy[i]>Level && heightcopy[j]>Level) {
                Level++;

            }
            
        }
            
        int sumTotal = accumulate(heightcopy.begin(), heightcopy.end(), 0);  
        int sumHeight = accumulate(height.begin(),height.end(),0);
        
        
        return sumTotal-sumHeight;
        
    }

Problem solution in C.

int trap(int* height, int heightSize){
    int leftMax=0, rightMax=0, left=0, right=heightSize-1, result=0, max;
    
    while(left < right) {
        if(height[left]>leftMax) leftMax = height[left];
        if(height[right]>rightMax) rightMax = height[right];      
        if(leftMax<rightMax) {
            if((leftMax-height[left]) > 0) result += (leftMax-height[left]); 
            left++;
        } else {
            if((rightMax-height[right]) > 0) result += (rightMax-height[right]);
            right--;
        }
    }
    
    return result;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes