Ctrl K

Striped Scarf

Medium
A scarf is knitted from stripes whose colours are stored in the array colors (colours are positive integers). The scarf is properly striped when two patterns strictly take turns: - colors[i - 2] == colors[i] for every 2 ≤ i < n; - colors[i - 1] != colors[i] for every 1 ≤ i < n. In one operation you can repaint any stripe with any positive colour. Return the minimum number of repaints that makes the scarf properly striped.
Example 1
colors=[4, 2, 4, 4, 1, 2] → 2

Keep 4 on even places and 2 on odd ones: repaint stripe 3 (4 → 2) and stripe 4 (1 → 4). 2 repaints in total.

Example 2
colors=[7, 7, 7, 7] → 2

Neighbouring stripes may not share a colour, so the stripes at odd places have to be repainted with another colour: 2 stripes.

Example 3
colors=[3, 5, 3, 5, 3] → 0
Constraints
  • 1 ≤ n = len(colors) ≤ 10⁵
  • 1 ≤ colors[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumSpread Out the Labels MediumPick Within Tag Limits MediumFewest Kinds After Removals MediumBalance the Dice Rows Medium
loading Python…
Input
colors=[4, 2, 4, 4, 1, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 135 tests on the server, hidden ones included.