only cost linear space memory