-
Notifications
You must be signed in to change notification settings - Fork 4
/
trapping-rain-water.js
61 lines (55 loc) · 1.29 KB
/
trapping-rain-water.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var DEBUG = process.env.DEBUG;
/**
* @param {number[]} height
* @return {number}
*/
var trap1 = function(height) {
var upper = 0;
for (var i = 0; i < height.length; i++)
if (height[i] > upper) upper = height[i];
var ans = 0;
for (var curr = 1; curr <= upper; curr++) {
var temp = height.map(function (h) {
return h >= curr ? 1 : 0;
});
var lo = 0;
while (temp[lo] !== 1 && lo !== temp.length) lo++;
var hi = temp.length - 1;
while (temp[hi] !== 1 && hi >= 0) hi--;
if (hi === lo || hi < 0 || lo === temp.length) break;
for (var i = lo; i < hi; i++)
ans += temp[i] === 0 ? 1 : 0;
}
return ans;
};
var trap = function(height) {
var f = {}, g = {};
var h = 0;
for (var i = 0; i < height.length; i++) {
f[i] = h;
h = Math.max(h, height[i]);
}
h = 0;
for (var i = height.length - 1; i >= 0; i--) {
g[i] = h;
h = Math.max(h, height[i]);
}
var ans = 0;
for (var i = 0; i < height.length; i++) {
h = Math.min(f[i], g[i]);
if (height[i] < h)
ans += h - height[i];
}
return ans;
};
function test(f) {
[
[[0,1,0,2,1,0,1,3,2,1,2,1]],
[[2,0,2,1,2]],
[[4]],
[[199*199*199,0, 199*199*199]]
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(trap);