-
Notifications
You must be signed in to change notification settings - Fork 1
/
42.js
31 lines (26 loc) · 797 Bytes
/
42.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
// // 接雨水
//
function trap(height) {
let result = 0;
for (let i = 0; i < height.length; i++) {
result += singleTrapWater(height, i);
}
return result;
// 求单格的盛水量
function singleTrapWater(heightArr, index) {
let leftArr = [];
let rightArr = [];
if (index === 0 || index === heightArr.length - 1) {
return 0
}
for (let i = 0; i < index; i++) {
leftArr.push(heightArr[i]);
}
for (let j = index + 1; j < heightArr.length; j++) {
rightArr.push(heightArr[j])
}
return Math.min(Math.max(...leftArr), Math.max(...rightArr)) - heightArr[index]
}
}
console.log(trap([0,1,0,2,1,0,1,3,2,1,2,1]));
// console.log([3,2,1].sort().reverse()[0]);