ZigZag solved with extra memory