You are given an integer array lights of length n, representing positions 0 through n - 1 on a road.
For each position i:
lights[i] = v, where v > 0, there is a working bulb at position i that illuminates every position from max(0, i - v) to min(n - 1, i + v), inclusive.lights[i] = 0, there is no working bulb at position i.A position is visible if it is illuminated by at least one working bulb.
You may install additional bulbs at any positions. Each additional bulb installed at position j illuminates positions from max(0, j - 1) to min(n - 1, j + 1), inclusive.
Return the minimum number of additional bulbs required to make every position on the road visible.
Example 1:
Input: lights = [0,0,0,0]
Output: 2
Explanation:
One optimal placement is:
[0, 1, 2].[2, 3].Therefore, the minimum number of additional bulbs required is 2.
Example 2:
Input: lights = [0,0,0,2,0]
Output: 1
Explanation:
lights[3] = 2, the working bulb at position 3 illuminates positions [1, 2, 3, 4].[0, 1, 2], making every position visible.
Constraints:
1 <= n == lights.length <= 1050 <= lights[i] <= nProblem Overview: You are given a road represented as an array where some positions can host a light. Each light illuminates a fixed range around its position. The task is to choose the minimum number of lights so every road segment is covered, or return -1 if full illumination is impossible.
Approach 1: Brute Force Coverage Simulation (O(n^2) time, O(1) space)
Start from the leftmost dark position and check every candidate light within its coverage window. For each possible light position, simulate turning it on and mark the range it illuminates. Move forward to the next uncovered position and repeat. This approach directly follows the problem definition but repeatedly scans overlapping ranges, which leads to quadratic time in the worst case. It is useful for understanding the coverage rules but becomes slow for large roads.
Approach 2: Greedy Farthest Reach (O(n) time, O(1) space)
The optimal strategy is greedy: always activate the light that covers the current dark position and extends coverage the farthest to the right. When you are at index i, look within the valid window [i - B + 1, i + B - 1] for a working light. Choose the rightmost available light because it maximizes the next illuminated boundary. After turning it on, jump directly to the first position beyond its coverage. This works because any earlier light would produce strictly smaller coverage, which could only increase the total number of lights needed. The algorithm scans the road once while adjusting the search window, making it linear time.
Approach 3: Interval Coverage Interpretation (O(n log n) time, O(n) space)
Each valid light can be viewed as an interval [i - B + 1, i + B - 1]. Generate all such intervals for positions where a light can be installed. Sort intervals by their start point and greedily select the interval that extends coverage the farthest while still covering the current position. This mirrors the classic interval covering problem often seen with greedy algorithms and interval problems. While conceptually clean, sorting adds extra overhead compared with the direct linear scan.
Recommended for interviews: The greedy farthest‑reach solution is what interviewers expect. It demonstrates that you recognize this as a coverage optimization problem similar to interval scheduling. Mentioning the brute force method first shows you understand the constraints, but implementing the O(n) greedy scan over the array demonstrates strong problem‑solving instincts and optimal complexity.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Coverage Simulation | O(n^2) | O(1) | Useful for understanding the problem or validating small test cases |
| Interval Coverage with Sorting | O(n log n) | O(n) | When modeling lights as intervals or explaining greedy interval coverage |
| Greedy Farthest Reach | O(n) | O(1) | Optimal solution for interviews and production implementations |
Practice Minimum Lights to Illuminate a Road with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor