Skip to content

Instantly share code, notes, and snippets.

@meetkool
Created October 9, 2025 16:06
Show Gist options
  • Save meetkool/e60f7f596d6a1a4d898a6db9965051de to your computer and use it in GitHub Desktop.
Save meetkool/e60f7f596d6a1a4d898a6db9965051de to your computer and use it in GitHub Desktop.
[BLOG] Untitled Post
title location images tags privacy createdAt
Untitled Post
19.2053, 72.9481
blog
quick-post
public
2025-10-09 16:06:52 UTC

https://leetcode.com/problems/trapping-rain-water/ Method-1 two pointer

class Solution {
public:
    int trap(vector<int>& height) {
        		int leftmax=0,rightmax=0,l=0,r=height.size()-1;

          int total=0;

          while(l<r){
               if(height[l]<=height[r]){
                    if(leftmax>height[l]){
                         total += leftmax -height[l];
                    }else{
                        leftmax = max(leftmax,height[l]); 
                    }
                    l++;
               }
               else{
                    if(rightmax>height[r]){
                         total += rightmax -height[r];
                    }else {
                         rightmax = max(rightmax,height[r]); 
                    }
                    r--;
               }
          }

          return total;
    }
};

method-2 ; prifix and suffix max:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment