greedy algorithm logic 568886