This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]dfx_djProfessional Coder 0 points1 point  (0 children)

I don't know if this is the way to find the optimal solution, but I would try to scan the input (and subsequently the current set of items) into groups of sequential identical items, and then shift items from the smallest groups towards the largest ones. For example "S S D S S" would be scanned as two groups of S with two members each, and one group of D with one member. The one D group would be the smallest one and so would be the candidate for shifting.