Skip to main content

Minimum Energy to Maintain Brightness - Solution & Explanation

MediumArraySorting10 min read
Practice this problem

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 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 <= 106
  • 1 <= brightness <= n
  • 1 <= intervals.length <= 105
  • intervals[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:

  1. Merge Overlapping Intervals: Merge all intervals that intersect with each other to obtain a set of mutually disjoint continuous intervals.
  2. Calculate Length Contribution: For each merged interval [start, end], the number of integer points (i.e., positions) it covers is m = 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
  3. 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n * t)O(1)Useful for understanding the process when constraints are small
Greedy Deficit CalculationO(n)O(1)Best general solution when total duration is known in advance
Priority Queue MaintenanceO(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 Larry173 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Minimum Energy to Maintain Brightness easy or hard?
The problem is generally considered Medium difficulty. The challenge lies in recognizing that the time-based simulation can be converted into a direct deficit calculation, reducing the complexity from O(n * t) to O(n).
Minimum Energy to Maintain Brightness Python/Java solution
Implement a single pass through the brightness array. For each lamp, compute max(0, t - brightness[i]) and accumulate the result. This logic translates directly into Python, Java, or C++ with a simple loop and integer arithmetic.
How to solve Minimum Energy to Maintain Brightness in O(n)?
Iterate through each lamp's brightness value and compare it with the required duration t. If brightness[i] is less than t, add the difference (t − brightness[i]) to the energy cost. Summing these deficits across the array yields the minimum energy needed in linear time.
What is the best approach for Minimum Energy to Maintain Brightness?
The most efficient approach is a greedy deficit calculation. Each lamp must have at least t brightness to survive t minutes if brightness decreases by 1 per minute. The minimum energy is the sum of max(0, t − brightness[i]) across all lamps. This runs in O(n) time and O(1) space.
Is Minimum Energy to Maintain Brightness asked at Google/Amazon/Meta?
Problems with greedy deficit calculations and resource maintenance frequently appear in interviews at companies like Google, Amazon, and Meta. While the exact problem title may vary, the underlying pattern of minimizing adjustments to meet constraints is common.
What data structure is used in Minimum Energy to Maintain Brightness?
The optimal solution typically uses only an array traversal with constant extra space. Some variations use a priority queue (min-heap) to always recharge the lamp closest to depletion when decisions must be made dynamically.
What is the time complexity of Minimum Energy to Maintain Brightness?
The optimal greedy solution runs in O(n) time because you only scan the brightness array once and compute deficits. Space complexity is O(1) since only a running total of required energy is maintained.

Ready to solve this problem?

Practice Minimum Energy to Maintain Brightness with our built-in code editor and test cases.

Practice on FleetCode