linear space solution