10000
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 e0195df commit c9a851cCopy full SHA for c9a851c
array.js
@@ -28,3 +28,30 @@ var maxArea = function (height) {
28
}
29
return maxWater
30
};
31
+
32
+// trapping rain water
33
+var trap = function(height) {
34
+ if (height.length === 0) return 0;
35
+ // two pointer and correspoding variables
36
+ let left = 0, leftMax = 0;
37
+ let right = height.length - 1, rightMax = 0;
38
+ let waterTrapped = 0;
39
+ while (left < right){
40
+ if (height[left] < height[right]){
41
+ if (height[left] >= leftMax){
42
+ leftMax = height[left];
43
+ }else{
44
+ waterTrapped += leftMax - height[left]
45
+ }
46
+ left++;
47
48
+ if (height[right] >= rightMax){
49
+ rightMax = height[right];
50
51
+ waterTrapped += rightMax - height[right]
52
53
+ right--;
54
55
56
+ return waterTrapped;
57
+};
0 commit comments