Skip to main content
added 340 characters in body
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

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 element x is stackable if r is stackable and either x is zero or both x-1 appears in r and r with x-1 removed is also stackable.

Haskell, 77 bytes

import Data.List
g[]=1<3
g(x:r)=g r&&(x<1||elem(x-1)r&&g(r\\[x-1]))
g.reverse

Try it online! Usage: g.reverse $ [0,1,2]. Returns True for stackable inputs and False otherwise.

Haskell, 77 75 bytes

import Data.List
g[]=1<3
g(x:r)|s<-r\\[x-1]=g r&&(x<1||s/=r&&g s)
g.reverse

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 element x is stackable if r is stackable and either x is zero or both x-1 appears in r and r with x-1 removed is also stackable.
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

Haskell, 77 bytes

import Data.List
g[]=1<3
g(x:r)=g r&&(x<1||elem(x-1)r&&g(r\\[x-1]))
g.reverse

Try it online! Usage: g.reverse $ [0,1,2]. Returns True for stackable inputs and False otherwise.