space complexity optimization