给定n个非负整数表示每个宽度为1的柱子的高度题,计算按此排列的珠子,下雨之后能接多少雨水。
var str = readline();
var arr=str.split(',');
var num=trap(arr);
console.log(num)
function trap (height) {
let left = 0, right = height.length - 1
let count = 0
let leftMax = 0, rightMax = 0
while (left <= right) {
leftMax = Math.max(leftMax, height[left])
rightMax = Math.max(rightMax, height[right])
if (leftMax < rightMax) {
count += leftMax - height[left]
left++
} else {
count += rightMax - height[right]
right--
}
}
return count
};