JSFiddle - React, Tailwind, and code Playground

JavaScript

console.clear();

var solvePuddles = function(hills, isReverse) {
    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;
        }
    }
    if(vol > 0 && !isReverse) {
       var revHills = hills.reverse();
       volumes.push(solvePuddles(revHills, true)[0]);
    }
    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];
var input4 = [2, 6, 1, 2, 3, 4, 4, 5, 4];

console.log('input1', solvePuddles(input1));
console.log('input2', solvePuddles(input2));
console.log('input3', solvePuddles(input3));
console.log('input4', solvePuddles(input4));