Minimum Energy to Maintain Brightness Problem
Description
LeetCode Problem 3951.
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] = [start_i, end_i] 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 is 2 * 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 <= 10^61 <= brightness <= n1 <= intervals.length <= 10^5intervals[i] == [start_i, end_i]0 <= start_i <= end_i <= 10^9
Solution
Approach: Interval Merge
One bulb lights at most 3 positions (itself and its two neighbours), so reaching brightness brightness needs at least ceil(brightness / 3) bulbs on, and that many always suffice. Every time unit covered by some interval costs exactly that much energy, so the answer is that cost times the number of covered time units.
To count covered time units without double counting, sort the intervals by start and merge the overlapping ones. For each merged interval [start, end] add ceil(brightness / 3) * (end - start + 1).
Complexity: time O(m log m), space O(m), where m is the number of intervals.
class Solution:
def minEnergy(self, n: int, brightness: int, intervals: List[List[int]]) -> int:
intervals.sort()
merged = [intervals[0]]
for x in intervals[1:]:
if merged[-1][1] < x[0]:
merged.append(x)
else:
merged[-1][1] = max(merged[-1][1], x[1])
ans = 0
for start, end in merged:
m = end - start + 1
ans += (brightness + 2) // 3 * m
return ans