-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_030.js
42 lines (39 loc) Β· 1.18 KB
/
problem_030.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Returns the units of water remain trapped on the map
* @param {number[]} blockHeights
* @return {number}
*/
function captureRain(blockHeights) {
// Left & Right pointers at beginning and end
let left = 0;
let right = blockHeights.length - 1;
let totalWater = 0;
let currMaxLeftHeight = 0;
let currMaxRightHeight = 0;
while(left < right) {
if (blockHeights[left] <= blockHeights[right]) {
// there cannot be water trapped
if (blockHeights[left] >= currMaxLeftHeight){
currMaxLeftHeight = blockHeights[left];
} else {
const currWaterTrapped = currMaxLeftHeight - blockHeights[left];
totalWater += currWaterTrapped;
}
// Move left pointer forward
left++;
} else {
// there cannot be water trapped
if(blockHeights[right] >= currMaxRightHeight) {
currMaxRightHeight = heights[right];
} else {
const currWaterTrapped = currMaxRightHeight - blockHeights[right];
totalWater += currWaterTrapped;
}
// Move right pointer left
right--;
}
}
return totalWater;
}
console.log(captureRain([2,1,2])); // 1
console.log(captureRain([3, 0, 1, 3, 0, 5])); // 8