Java Stone Blocks
public class Solution {
public static int stoneBlocks(int[] h) {
int[] height = new int[h.length];
int index = 0;
int blocks = 0;
for (int i : 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.