Analysis updated for space complexity.