FunctionRacerProblemsRace

Flip K Lamps at a Time

HardArraysPython, JavaScript, TypeScript, Java, C++

lamps is a row of lamps: 1 means on, 0 means off. One move picks k lamps that stand next to each other and flips all k of them, so each on lamp goes off and each off lamp goes on.

Return the fewest moves that leave every lamp on, or -1 if that cannot be done.

Example 1

lamps [0,0,1,0,0,1]
k 3
returns 2

Flip lamps 0 to 2 (now 1, 1, 0, 0, 0, 1), then lamps 2 to 4.

Example 2

lamps [1,0,1]
k 2
returns -1

With k = 2 every move changes two lamps, so the number of off lamps stays odd.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems