Ctrl K

Bright Enough in Every Triple

Medium
A row of street lamps runs along a street, and levels[i] is the brightness of lamp i. The street counts as lit when **every three consecutive lamps** include at least one whose brightness is at least need. In one step you can pick any lamp and raise its brightness by 1. Brightness can never be lowered. Return the smallest number of steps that makes the street lit. Note that once every run of three consecutive lamps is fine, every longer run is fine too.
Example 1
levels=[1, 4, 0, 2, 0, 1], need=3 → 1

The triples [1, 4, 0] and [4, 0, 2] are already covered by the lamp at 4. The triples [0, 2, 0] and [2, 0, 1] both contain the lamp at index 3, so raising it from 2 to 3 costs one step.

Example 2
levels=[0, 0, 0, 0, 0, 0, 0], need=2 → 4

Making the lamps at indices 2 and 5 bright is enough, since every run of three touches one of them. That is 2 + 2 = 4 steps.

Example 3
levels=[7, 1, 1], need=5 → 0

The only triple already holds a lamp at 7, so nothing needs to change.

Constraints
  • 3 ≤ len(levels) ≤ 10⁵
  • 0 ≤ levels[i] ≤ 10⁹
  • 0 ≤ need ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
levels=[1, 4, 0, 2, 0, 1], need=3
Expected
1

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