Elixir Stone Blocks
defmodule StoneBlocks do
def stone_blocks(h) do
{_stack, blocks} =
Enum.reduce(h, {[], 0}, fn height, {stack, blocks} ->
stack = Enum.drop_while(stack, &(&1 > height))
case stack do
[^height | _] -> {stack, blocks}
_ -> {[height | stack], blocks + 1}
end
end)
blocks
end
end
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.