-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap.js
More file actions
executable file
·53 lines (36 loc) · 1.26 KB
/
trap.js
File metadata and controls
executable file
·53 lines (36 loc) · 1.26 KB
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
43
44
45
46
47
48
49
50
51
52
53
// anchor problem: Trapping rain water
// function to calculate trapped rain water
// what is trapping rain water ?
// trapping rain water is a problem where we need to find the amount of water that can be trapped between the bars of different heights after raining.
// example :
// input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
// output: 6
// explanation: the amount of water that can be trapped is 6 units.
function trap(height) {
let left = 0;
let right = height.length - 1;
let leftMax = 0;
let rightMax = 0;
let waterTrapped = 0;
while (left < right) {
if (height[left] < height[right]) {
leftMax = Math.max(leftMax, height[left]);
waterTrapped += leftMax - height[left];
left++;
} else {
rightMax = Math.max(rightMax, height[right]);
waterTrapped += rightMax - height[right];
right--;
}
}
return waterTrapped;
}
// test the function
console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1])); // Output: 6
console.log(trap([4,2,0,3,2,5])); // Output: 9
console.log(trap([1,0,2,1,0,1,3])); // Output: 5
console.log(trap([5,4,1,2])); // Output: 1
console.log(trap([2,0,2])); // Output: 2
console.log(trap([3,0,0,2,0,4])); // Output: 10
console.log(trap([0,0,0,0])); // Output: 0
console.log(trap([1,2,3,4,5])); // Output: 0