Minimum Energy to Maintain Brightness - Solution & Explanation
Problem Statement
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:
- Turn on the light bulbs at positions 1 and 4.
- Current state of line:
0 1 0 0 1. - All 5 positions are illuminated, so the required brightness is reached.
- The active interval has length
12 - 6 + 1 = 7, so the total energy is2 * 7 = 14.
Example 2:
Input: n = 2, brightness = 1, intervals = [[0,0],[2,2]]
Output: 2
Explanation:
- Turn on one light bulb during each active interval.
- Each interval has length 1, so the total active time is
1 + 1 = 2. - The total energy is
1 * 2 = 2.
Example 3:
Input: n = 4, brightness = 2, intervals = [[1,3],[2,4]]
Output: 4
Explanation:
- Turn on one light bulb. It can illuminate at least 2 positions.
- The active intervals overlap, so the total active time is the length of
[1,4], which is 4. - The total energy is
1 * 4 = 4.
Constraints:
1 <= n <= 1061 <= brightness <= n1 <= intervals.length <= 105intervals[i] == [starti, endi]0 <= starti <= endi <= 109
Approach Overview
Problem 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.
Solution
A single bulb can illuminate at most 3 positions. To ensure the total brightness is at least brightness, the number of bulbs required to be turned on is \lceil \frac{brightness}{3} \rceil. In programming, this is commonly written in integer division form as (brightness + 2) / 3.
This problem can be solved through the following steps:
- Merge Overlapping Intervals: Merge all intervals that intersect with each other to obtain a set of mutually disjoint continuous intervals.
- Calculate Length Contribution: For each merged interval
[start, end], the number of integer points (i.e., positions) it covers ism = end - start + 1. Since every position within the interval must satisfy the minimum brightness, the total energy required for this interval is: $Energy = \lceil \frac{brightness}{3} \rceil times m - Accumulate and Sum: Accumulate the energy of all disjoint intervals to get the final answer ans
.
The time complexity is O(n log n), and the space complexity is O(n), where n$ is the number of intervals.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
3951. Minimum Energy to Maintain Brightness (Leetcode Medium) • Programming Live with Larry • 173 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Minimum Energy to Maintain Brightness easy or hard?
Minimum Energy to Maintain Brightness Python/Java solution
How to solve Minimum Energy to Maintain Brightness in O(n)?
What is the best approach for Minimum Energy to Maintain Brightness?
Is Minimum Energy to Maintain Brightness asked at Google/Amazon/Meta?
What data structure is used in Minimum Energy to Maintain Brightness?
What is the time complexity of Minimum Energy to Maintain Brightness?
Ready to solve this problem?
Practice Minimum Energy to Maintain Brightness with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor