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.
1 parent 6562c33 commit 32379daCopy full SHA for 32379da
trapping-rain-water/trapping-rain-water.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ int trap(vector<int>& a) {
4
+
5
+ int res = 0;
6
+ int n = a.size();
7
+ int lmax[n];
8
+ int rmax[n];
9
10
+ lmax[0] = a[0];
11
+ for(int i=1;i<n;i++) {
12
+ lmax[i]=max(a[i],lmax[i-1]);
13
+ }
14
15
+ rmax[n-1] = a[n-1];
16
+ for(int i=n-2;i>0;i--) {
17
+ rmax[i]=max(a[i],rmax[i+1]);
18
19
20
21
+ res = res + (min(lmax[i],rmax[i])-a[i]);
22
23
24
+ return res;
25
26
+};
0 commit comments