Go Stone Blocks
func stoneBlocks(h []int) int {
height := make([]int, 0, len(h))
blocks := 0
for _, v := range h {
for len(height) > 0 && height[len(height)-1] > v {
height = height[:len(height)-1]
}
if len(height) > 0 && height[len(height)-1] == v {
continue
}
height = append(height, v)
blocks++
}
return blocks
}
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.