You are given an integer n, representing n light bulbs arranged in a line and indexed from 0 to n - 1.
You are also given an integer brightness and a 2D integer array intervals, where intervals[i] = [starti, endi] represents an inclusive time interval during which the lighting requirement must be satisfied.
At each time unit, every bulb can independently be either on or off. A bulb that is on illuminates its own position and its adjacent positions, if they exist.
The total illumination at a time unit is the number of illuminated positions. Each position is counted at most once.
For every integer time unit covered by at least one interval in intervals, the total illumination must be at least brightness. At time units not covered by any interval, all bulbs may remain off. Each bulb that is on consumes 1 unit of energy for that time unit.
Return an integer denoting the minimum total energy required.
Example 1:
Input: n = 5, brightness = 5, intervals = [[6,12]]
Output: 14
Explanation:
0 1 0 0 1.12 - 6 + 1 = 7, so the total energy is 2 * 7 = 14.Example 2:
Input: n = 2, brightness = 1, intervals = [[0,0],[2,2]]
Output: 2
Explanation:
1 + 1 = 2.1 * 2 = 2.Example 3:
Input: n = 4, brightness = 2, intervals = [[1,3],[2,4]]
Output: 4
Explanation:
[1,4], which is 4.1 * 4 = 4.
Constraints:
1 <= n <= 1061 <= brightness <= n1 <= intervals.length <= 105intervals[i] == [starti, endi]0 <= starti <= endi <= 109Problem Overview: You are given the brightness of several lamps and a required duration they must stay lit. Brightness decreases over time, and you can spend energy to increase brightness. The task is to compute the minimum total energy required so that every lamp stays above zero brightness for the entire duration.
Approach 1: Brute Force Simulation (O(n * t) time, O(1) space)
Simulate the process minute by minute. At each step, decrease the brightness of every lamp and check if any lamp reaches zero. When a lamp is about to go dark, add energy to increase its brightness. This method directly models the process but performs repeated updates for every time unit, which becomes expensive when the required duration is large.
Approach 2: Greedy Deficit Calculation (O(n) time, O(1) space)
Instead of simulating each minute, compute how much brightness each lamp needs to survive the full duration. If a lamp starts with brightness b and must last t minutes while losing 1 unit per minute, it needs at least t brightness initially. Any deficit max(0, t - b) must be supplied as energy. Summing this deficit across all lamps gives the minimum energy required. This works because adding energy earlier or later produces the same total effect.
Approach 3: Priority Queue Maintenance (O(n log n) time, O(n) space)
If the problem allows selective recharging during the timeline, a greedy strategy with a min-heap can track which lamp will run out of brightness first. Always recharge the lamp closest to depletion. The heap stores remaining brightness and ensures the smallest value is handled first. This pattern appears frequently in greedy scheduling and priority queue problems where maintaining system stability requires handling the most critical element first.
Recommended for interviews: The deficit-based greedy solution is the expected approach. The brute-force simulation shows you understand the mechanics of the problem, but the optimized method demonstrates that you can convert a time-based process into a direct mathematical calculation. Recognizing this transformation is a common interview signal in array and greedy optimization problems.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n * t) | O(1) | Useful for understanding the process when constraints are small |
| Greedy Deficit Calculation | O(n) | O(1) | Best general solution when total duration is known in advance |
| Priority Queue Maintenance | O(n log n) | O(n) | When recharging decisions must be made dynamically over time |
Practice Minimum Energy to Maintain Brightness with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor