↓Skip to main content
  1. LeetCode/

LeetCode 3951: Minimum Energy to Maintain Brightness

·2 mins· ·
LeetCode Medium Array Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-11

  • Submissions: Accepted (first try)

Approach
#

Split it into two independent parts:

  1. Bulbs per time unit: a bulb lights itself and its two neighbors, at most 3 positions, so at least ceil(brightness / 3) bulbs are needed. That many always suffices: place bulbs at 1, 4, 7, …, each lighting 3 positions with no overlap, and move the last one to n - 1 if it falls off the end, lighting min(3k, n) >= brightness positions (the problem guarantees brightness <= n).
  2. Which time units: those covered by at least one interval. Intervals may overlap, so sort, merge, and sum the lengths (Array Template 5).

The answer is their product.

My Solution
#

class Solution:
    def minEnergy(self, n: int, brightness: int, intervals: List[List[int]]) -> int:
        bulbs = (brightness + 2) // 3                  # ceil(brightness / 3)
        total = 0
        cur_s, cur_e = None, None
        for s, e in sorted(intervals):
            if cur_e is not None and s <= cur_e:       # 重疊(包含共用端點)就合併
                cur_e = max(cur_e, e)
            else:
                if cur_e is not None:
                    total += cur_e - cur_s + 1
                cur_s, cur_e = s, e
        total += cur_e - cur_s + 1
        return total * bulbs

Complexity
#

Time O(m log m) for m intervals, space O(m) (sorting).

Mistakes to Avoid
#

  • Intervals are inclusive integer times of length e - s + 1; a shared endpoint counts as overlapping when merging.
  • Write ceil(b / 3) in integers as (b + 2) // 3.

Interview Explanation
#

Each bulb lights at most 3 positions, so each time unit needs ceil(brightness / 3) bulbs, which can always be arranged. The time needing light is the union of the intervals, which I sort and merge for its total length, then multiply by the bulbs per unit. Time O(m log m).

Related

LeetCode 522: Longest Uncommon Subsequence II
·2 mins
LeetCode Medium Two-Pointers String Sorting Array Hash-Table
LeetCode 522 note: the longest uncommon subsequence is always a whole string; check strings from longest to shortest, and the first that is not a subsequence of any other long-enough string is the answer
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 3323: Minimize Connected Groups by Inserting Interval
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3323 note: merge first; a new interval can join groups l through r ⇔ start of group r − end of group l <= k, so a sliding window finds the most groups one interval can connect
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 1040: Moving Stones Until Consecutive II
·3 mins
LeetCode Medium Sliding-Window Math Sorting Array
LeetCode 1040 note: max moves = total gaps minus the edge gap the first move wastes; min moves uses a length-n window holding the most stones, with a special case of 2 when n - 1 stones are already consecutive and one is far away