We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 44960b8 commit c06ad1cCopy full SHA for c06ad1c
0042-trapping-rain-water/0042-trapping-rain-water.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ public int trap(int[] height) {
3
+ int[] left = new int[height.length];
4
+ int maxL = 0;
5
+ int[] right = new int[height.length];
6
+ int maxR = 0;
7
+ for (int i = 0; i < left.length; i++) {
8
+ maxL = Math.max(maxL, height[i]);
9
+ left[i] = maxL;
10
+ }
11
+ for (int i = right.length - 1; i >= 0; i--) {
12
+ maxR = Math.max(maxR, height[i]);
13
+ right[i] = maxR;
14
15
+ // calculate water trap
16
+ int water = 0;
17
+ for (int i = 0; i < height.length; i++) {
18
+ int min = Math.min(left[i], right[i]);
19
+ int currWater = min - height[i];
20
+ water = water + currWater;
21
22
+ return water;
23
24
+}
0 commit comments