Arranging Shapes
Time limit1sMemory limit128 MB
Given a sequence of three shape types, compute the minimum swaps needed to group each type into one contiguous block, in any order of blocks.
- Level
Medium6 of 10
- Topics
- Sliding window, Greedy, Array
- Solved
- No attempts yet
Problem
Triangles, squares, and circles are arranged in a line. In one operation, choose any two positions and swap the shapes at those positions. The goal is to rearrange the line so that each kind of shape occupies one contiguous block. The order of the three blocks does not matter.
Given the current order of the shapes, write a program that finds the minimum number of swaps needed to make equal shapes contiguous.
Input
The first line contains the total number of shapes . is between and , inclusive.
The second line contains integers separated by spaces, representing the shapes in order. Integer represents a triangle, integer represents a square, and integer represents a circle.
Each of the three kinds of shape appears at least once.
Output
Print the minimum number of swaps required to make equal shapes contiguous.