Given a string of red and blue balls, find min number of swaps to club the colors together

Posted by efficiencyIsBliss on Stack Overflow See other posts from Stack Overflow or by efficiencyIsBliss
Published on 2011-01-11T04:31:13Z Indexed on 2011/01/11 4:54 UTC
Read the original article Hit count: 258

We are given a string of the form: RBBR, where R - red and B - blue.

We need to find the minimum number of swaps required in order to club the colors together. In the above case that answer would be 1 to get RRBB or BBRR.

I feel like an algorithm to sort a partially sorted array would be useful here since a simple sort would give us the number of swaps, but we want the minimum number of swaps.

Any ideas?

This is allegedly a Microsoft interview question according to this.

© Stack Overflow or respective owner

Related posts about arrays

Related posts about algorithm