this uses greedy algorithm