Ctrl K

Brightest Run After Each Outage

Hard
A row of n street lamps lines an alley; lamp i has brightness brightness[i]. At first all of them are on. Then the lamps go out one at a time in the order given by order: at step j, lamp order[j] switches off. Each lamp goes out exactly once. A **run** is a maximal stretch of adjacent lamps that are still on. The brightness of a run is the sum of its lamps' brightness. Return an array answer of length n where answer[j] is the largest run brightness right after step j (0 if no lamp is on).
Example 1
brightness=[4, 1, 3, 2, 6], order=[2, 0, 4, 1, 3] → [8, 8, 2, 2, 0]

Lamp 2 goes out: the lit runs are [4, 1] and [2, 6], the best is 8. Lamp 0: [1] and [2, 6], still 8. Lamp 4: [1] and [2], so 2. Lamp 1: only [2] is left, 2. The last lamp goes out: 0.

Example 2
brightness=[7], order=[0] → [0]

The only lamp goes out and no lit runs remain.

Constraints
  • 1 ≤ n = len(brightness) = len(order) ≤ 10⁵
  • 1 ≤ brightness[i] ≤ 10⁹
  • order is a permutation of 0 … n − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRichest Plot Under the Cap HardFlowers in Bloom on Visit Day HardThe Most Lit Spot on the Street MediumCity Block Outline HardClose Readings Nearby Hard
loading Python…
Input
brightness=[4, 1, 3, 2, 6], order=[2, 0, 4, 1, 3]
Expected
[8, 8, 2, 2, 0]

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