Grouping Objects greedy solution