Skip to content

Commit d6faaa1

Browse files
authored
Merge pull request #582 from gaurav2612gupta/patch-2
trapping rain water
2 parents 8dc9fbb + 67dcd9a commit d6faaa1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Trapping_Rain_water

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int trap(vector<int>& height) {
4+
int n = height.size();
5+
vector<int>l(n,height[0]);
6+
vector<int>r(n,height[n-1]);
7+
8+
int lm = height[0];
9+
for(int i = 1;i<n;i++){
10+
lm = max(lm,height[i]);
11+
l[i] = lm;
12+
}
13+
int rm = height[n-1];
14+
for(int i = n-2;i>=0;i--){
15+
rm = max(rm,height[i]);
16+
r[i] = rm;
17+
}
18+
19+
int ans = 0;
20+
for(int i = 0;i<n;i++){
21+
ans += (min(r[i],l[i])-height[i]);
22+
}
23+
24+
return ans;
25+
}
26+
};

0 commit comments

Comments
 (0)