Haskell Stone Blocks
stoneBlocks :: [Int] -> Int
stoneBlocks h = snd (foldl step ([], 0) h)
  where
    step (stack, blocks) i =
      case dropWhile (> i) stack of
        (top : rest) | top == i -> (top : rest, blocks)
        popped                  -> (i : popped, blocks + 1)

This uses a stack of active heights and only counts a new block when the wall needs a new height segment.