We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 8dc9fbb + 67dcd9a commit d6faaa1Copy full SHA for d6faaa1
Trapping_Rain_water
@@ -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