C++ Stone Blocks
#include <vector>
int stoneBlocks(const std::vector<int>& h)
{
std::vector<int> height;
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;
}
if (index == static_cast<int>(height.size())) {
height.push_back(i);
} else {
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.