Algorithms question, is my solution correct?