Package colors

Question

Suppose there are packages that come in three colors: red, green, and blue. When two packages of differing colors are set next to each other, it's possible to trade them in for a package of the third remaining color.

Given N packages, determine the smallest number of packages remaining after any possible sequence of such transformations.

For example, given the input ['R', 'G', 'B', 'G', 'B'], it is possible to end up with a single package through the following steps:

        Arrangement       |   Change
----------------------------------------
['R', 'G', 'B', 'G', 'B'] | (R, G) -> B
['B', 'B', 'G', 'B']      | (B, G) -> R
['B', 'R', 'B']           | (R, B) -> G
['B', 'G']                | (B, G) -> R
['R']                     |

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now