Ctrl K

Colors Spreading on a Ring

Medium
n lamps stand in a circle, and colors[i] is the color of lamp i. The neighbors of lamp i are lamps (i − 1 + n) % n and (i + 1) % n. Every second all lamps change at once: each lamp takes either its own current color or the color of one of its two neighbors, chosen independently for each lamp. Return the smallest number of seconds after which all lamps can show the same color.
Example 1
colors=[4, 9, 9, 4, 9, 9] → 1

Every color-4 lamp sits next to a color-9 lamp, so after one second all lamps can be color 9.

Example 2
colors=[1, 2, 3, 4, 5, 6, 7] → 3

Every color appears once. A color spreads by one lamp each way per second, so covering a ring of 7 lamps takes 3 seconds.

Example 3
colors=[3, 3, 3] → 0

All lamps already share a color.

Constraints
  • 1 ≤ len(colors) ≤ 10⁵
  • 0 ≤ colors[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
colors=[4, 9, 9, 4, 9, 9]
Expected
1

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