Haskell, 7777 75 bytes
import Data.List
g[]=1<3
g(x:r)=g r&&(x<1||elem(x|s<-1)r&&g(r\\[x-1])1]=g r&&(x<1||s/=r&&g s)
g.reverse
Try it online!Try it online! Usage: g.reverse $ [0,1,2]
. Returns True
for stackable inputs and False
otherwise.
This is a recursive solution which traverses a given list from back to front. It implements the observation that
- the empty list is stackable.
- a non-empty list with prefix
r
and last elementx
is stackable ifr
is stackable and eitherx
is zero or bothx-1
appears inr
andr
withx-1
removed is also stackable.