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.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.
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.
colors=[3, 3, 3] → 0
All lamps already share a color.
colors=[4, 9, 9, 4, 9, 9]
1
Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.