Lisp Stone Blocks
(defun stone-blocks (h)
(let* ((n (length h))
(height (make-array n :initial-element 0))
(index 0)
(blocks 0))
(dolist (i h)
(loop while (and (> index 0) (> (aref height (1- index)) i))
do (decf index))
(unless (and (> index 0) (= (aref height (1- index)) i))
(setf (aref height index) i)
(incf blocks)
(incf index)))
blocks))
This uses a stack of active heights and only counts a new block when the wall needs a new height segment.