const rainwater = function(heights) {
let total = 0;
if (heights.length < 3) {
return 0;
}
for (let i = 1; i < heights.length; i++) {
let currentNode = heights[i];
let maxL = 0;
let maxR = 0;
//find max right
for (let j = i+1; j < heights.length; j++) {
maxR = heights[j] > maxR ? heights[j] : maxR;
}
//find max left
//console.log('I: ' + i);
for (let j = i-1; j >= 0; j--) {
//console.log('J: ' + j);
maxL = heights[j] > maxL ? heights[j] : maxL;
}
if (Math.min(maxR,maxL) - currentNode > 0) {
total += Math.min(maxR,maxL) - currentNode;
}
//console.log('Max: ' + maxR + " Min: " + maxL + " Current Node: " + currentNode + " Total: " + total);
}
console.log(total);
return total;
}
rainwater([0,1,0,2,1,0,3,1,0,1,2])