PHP Stone Blocks
function stoneBlocks(array $h): int
{
$height = [];
$index = $blocks = 0;
foreach ($h as $i) {
while ($index > 0 && $height[$index - 1] > $i) {
$index--;
}
if ($index > 0 && $height[$index - 1] === $i) {
continue;
}
$height[$index] = $i;
$blocks++;
$index++;
}
return $blocks;
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.