Skip to content

Commit 46998b9

Browse files
authored
Merge pull request #15 from Arp1tSharma/patch-2
Create trapping-rain-water.cpp
2 parents ffd8090 + 9a8600a commit 46998b9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

trapping-rain-water.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int trap(vector<int>& height) {
4+
int n = height.size();
5+
vector<int> water(n,0);
6+
int highest = 0;
7+
for(int i = 0; i < n ; i++){
8+
if(highest <= height[i]){
9+
highest = height[i];
10+
}
11+
water[i] += (highest - height[i]);
12+
}
13+
highest = 0;
14+
int waterTrapped = 0;
15+
for(int i = n-1 ; i>=0 ; i--){
16+
if(highest < height[i]){
17+
highest = height[i];
18+
}
19+
water[i] = min(water[i] , highest - height[i]);
20+
waterTrapped += water[i];
21+
}
22+
return waterTrapped;
23+
}
24+
};

0 commit comments

Comments
 (0)