Wrote greedy algorithm