completed solution for greedy.