Skip to content

Commit 6631df8

Browse files
committed
feat: solve new question
1 parent a8cc8e1 commit 6631df8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

trapping-rain-water/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var trap = function(height) {
6+
let max_l = 0;
7+
let max_r = 0;
8+
let body_water_count = 0;
9+
10+
let l = 0;
11+
let r = height.length - 1;
12+
13+
while (l < r) {
14+
if (height[l] < height[r]) {
15+
if(height[l] > max_l) {
16+
max_l = height[l];
17+
} else {
18+
body_water_count += (max_l - height[l]);
19+
}
20+
l++;
21+
} else {
22+
if(height[r] > max_r) {
23+
max_r = height[r];
24+
} else {
25+
body_water_count += (max_r - height[r]);
26+
}
27+
r--;
28+
}
29+
}
30+
31+
return body_water_count;
32+
};

0 commit comments

Comments
 (0)