Ctrl K

A Clean Row of Lights

Medium
A row holds n lamps numbered 1 to n, all off at first. They are switched on one at a time: at step i (counting from 1) the lamp numbered order[i - 1] goes on. Each lamp is switched on exactly once, so order is a permutation of 1 to n. After step i the row looks clean if exactly lamps 1, 2, …, i are on and all the others are off. Return after how many steps the row looked clean.
Example 1
order=[2, 1, 4, 3] → 2

After step 2 lamps 1 and 2 are on, after step 4 all four are. After steps 1 and 3 there is a gap. The answer is 2.

Example 2
order=[3, 1, 2] → 1

Lamp 3 lights up first, so a clean row appears only at the very end.

Example 3
order=[1, 2, 3] → 3

The lamps go on in order, so the row is clean after every step: 3.

Constraints
  • 1 ≤ n = len(order) ≤ 5 · 10⁴
  • order is a permutation of 1 to n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
order=[2, 1, 4, 3]
Expected
2

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