Skip to content

Commit

Permalink
Merge pull request #101 from ritikpatel17/patch-5
Browse files Browse the repository at this point in the history
 Trapping Rain Water
  • Loading branch information
avastino7 authored Nov 1, 2022
2 parents 32cd9dd + 2f8b188 commit 6b35abf
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions trap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int trap(int[] height) {
int []rightGtr = new int[height.length];
int []leftGtr = new int[height.length];
//left greater
for(int i=0; i<leftGtr.length; i++) {
if(i==0) leftGtr[i]=height[i];
else
leftGtr[i] = Math.max(leftGtr[i-1], height[i]);
}
//right greater
for(int i=rightGtr.length-1; i>=0; i--) {
if(i==rightGtr.length-1) rightGtr[i]=height[i];
else
rightGtr[i] = Math.max(rightGtr[i+1], height[i]);
}
// System.out.println(Arrays.toString(rightGtr));
int water =0;
for(int i=0; i<height.length; i++) {
int level = Math.min(rightGtr[i], leftGtr[i]);//waterlevel
if(level>=height[i])
water += level-height[i];

}

return water;
}
}

0 comments on commit 6b35abf

Please sign in to comment.