Skip to content

Commit

Permalink
Q-42 (Day-502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagannath8 authored Apr 12, 2024
1 parent 4ca7ad8 commit 1ff2bea
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Trapping Rain Water.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int trap(int[] height) {
int ans = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
if (height[left] <= height[right]) {
int current = left;
while (height[++left] < height[current]) {
ans += height[current] - height[left];
}
}
else {
int current = right;
while (height[--right] < height[current]) {
ans += height[current] - height[right];
}
}
}
return ans;
}
}

0 comments on commit 1ff2bea

Please sign in to comment.