Trains the technique from
LeetCode 2134Minimum Swaps to Group All 1's Together IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A carousel carries trays in a circle. discs lists the trays in the order they come round, starting from tray 0, and the tray after the last one is tray 0 again. discs[i] is 1 when tray i carries a red disc and 0 when it carries a blue disc.
One swap takes the discs in any two trays and exchanges them. The two trays do not have to be next to each other.
The red discs are grouped when the trays carrying them form one unbroken run around the circle. Such a run is allowed to pass the last tray and continue from tray 0, and a run of no trays or of a single tray counts as unbroken.
Return the fewest swaps that leave the red discs grouped.
Example 1
The five red discs already sit on trays 5, 6, 7, 0 and 1, an unbroken run that passes the last tray and continues from tray 0, so no swap is needed.
Example 2
Swap the discs on trays 6 and 0, then swap the discs on trays 7 and 3. The five red discs then sit on trays 5, 6, 7, 8 and 9, an unbroken run.
Example 3
Swap the disc on tray 2 with the disc on tray 3. The red discs then sit on trays 3, 4 and 0, an unbroken run that passes the last tray.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fewest_swaps(discs: list[int]) -> int:public int fewestSwaps(int[] discs)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.