JSFiddle - React, Tailwind, and code Playground
JavaScript
console.clear();
function solvePuddles(hills) {
var peak = 0,
vol = 0,
volumes = [],
h, i;
for (i = 0; i < hills.length; i++) {
h = hills[i];
if (peak <= h) {
peak = h;
if (vol) {
volumes.push(vol);
vol = 0;
}
} else {
vol += peak - h;
}
}
return volumes;
}
var input1 = [2, 5, 1, 2, 3, 4, 7, 7, 6];
var input2 = [2, 5, 1, 3, 1, 2, 1, 7, 7, 6];
var input3 = [1, 2, 1, 1, 1, 1, 2, 1, 1, 2];
console.log('input1', solvePuddles(input1));
console.log('input2', solvePuddles(input2));
console.log('input3', solvePuddles(input3));