wrote greedy algorithm