TypeScript Stone Blocks
function stoneBlocks(h: number[]): number {
const height: number[] = [];
let index = 0;
let blocks = 0;
for (const i of h) {
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.