Ctrl K

Flickering Stretches

Medium
Street lamps stand in a row; lamps[i] is 1 if the lamp is on and 0 if it is off. A stretch of consecutive lamps is flickering if no two neighbouring lamps in it are in the same state. A stretch of a single lamp is flickering too. Return the number of flickering stretches. Two stretches are different if they differ in at least one end.
Example 1
lamps=[1, 1, 0, 1] → 7

The four single lamps count, plus [1, 0], [0, 1] and [1, 0, 1] at the end, for 7 in total.

Example 2
lamps=[0, 0, 0] → 3

Neighbouring lamps are all equal, so only the single-lamp stretches flicker.

Constraints
  • 1 ≤ len(lamps) ≤ 10⁵
  • lamps[i] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTurn a Square Clockwise MediumPostfix Calculator MediumRoll an Array to the Right MediumPrimes Below a Bound MediumNth Number Built From Given Primes Medium
loading Python…
Input
lamps=[1, 1, 0, 1]
Expected
7

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