You've been given a bag of Skittles. Everybody knows that in order to appreciate the different flavors the most, you need to rotate between the flavors.
Basics:
- You can only eat 1 skittle at a time
- The order that you eat your skittles must be periodic.
- Each period cannot contain a particular flavor more than once.
- Your bag only has so many skittles. You cannot eat more of a particular flavor of skittle than appears in your bag.
- You want to eat as many skittles as you can (it may not always be possible)
Examples:
Lets say you start with 3 Red, 2 Blue, and 3 Green skittles:
R B G R B G R G Invalid: The last R must be followed by a B, not a G
R B G R B G R Valid, but sub-optimal
R R R Valid, but sub-optimal
R G B R G B R G Valid and optimal
G R B G R B G R Also valid and optimal (there are multiple good solutions)
Input/Output
- You are passed a non-empty list of positive integers for the color counts. (The above example would be
[3,2,3]
). - You need to return a list containing valid and optimal ordering.
- Instead of using colors, you will use the indices from input list. (The last example output above would be
[2,0,1,2,0,1,2,0]
). - Your output may be 0-indexed or 1-indexed. My examples will be 0-indexed
Test Cases
1 0
4 0 0 0 0
4 1 0 0 0 0
3 1 0 1 0 or 0 0 0
5 2 2 0 1 2 0 1 2 0
2 3 5 2 1 0 2 1 0 2 1 or 1 2 0 1 2 0 1 2
2 4 5 2 1 2 1 2 1 2 1 2
3 4 5 2 1 0 2 1 0 2 1 0 2 1 or 1 2 0 1 2 0 1 2 0 1 2
1 1 1 1 1 6 5 0 1 2 3 4 5 (lots of other solutions)
1 1 1 1 1 8 5 5 5 5 5 5 5 5
2 4 6 8 3 2 1 3 2 1 3 2 1 3 2 1 3 2
This is a code-golf, so make your solutions as short as possible in your favorite language!