We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4635bb4 commit 8f96177Copy full SHA for 8f96177
TrappingRainWater/TrappingRainWater.cpp
@@ -0,0 +1,26 @@
1
+class Solution {
2
+public:
3
+ int trap(int A[], int n) {
4
+ // Start typing your C/C++ solution below
5
+ // DO NOT write int main() function
6
+
7
+ int max_from_left[n];
8
+ int max_from_right[n];
9
10
+ max_from_left[0] = A[0];
11
+ for (int i = 1; i < n; i++)
12
+ max_from_left[i] = max(max_from_left[i-1], A[i]);
13
14
+ max_from_right[n-1] = A[n-1];
15
+ for (int i = n - 2; i >= 0; i--)
16
+ max_from_right[i] = max(max_from_right[i+1], A[i]);
17
18
+ int water = 0;
19
+ for (int i = 1; i < n - 1; i++) {
20
+ int min_height = min(max_from_left[i-1], max_from_right[i+1]);
21
+ if (min_height > A[i])
22
+ water += min_height - A[i];
23
+ }
24
+ return water;
25
26
+};
0 commit comments