Most Overlapped Aisle Along a Line of Lamps
Greedy & IntervalsAuto-graded · 23 test casesLast updated
Find the integer position covered by the most symmetric intervals, returning the smallest on ties, using an event sweep over interval endpoints instead of visiting every position.
A long corridor is described by integer positions on a number line. You are given a list lamps, where each entry lamps[i] = [pos_i, radius_i] is a lamp placed at position pos_i. That lamp lights every integer position from pos_i - radius_i up to pos_i + radius_i, with both endpoints included.
The brightness of an integer position is the number of lamps that light it. Return the integer position with the greatest brightness. If more than one position reaches that greatest brightness, return the smallest of them.
Rules
- Only integer positions are considered.
- A lamp with
radius_i = 0lights only its own positionpos_i. - Several lamps may be placed at the same position; each one is counted separately.
lampsis never empty, so the maximum brightness is always at least 1 and an answer always exists.- Positions that no lamp lights have brightness 0 and are never the answer.
Constraints
1 <= len(lamps) <= 10^5-10^8 <= pos_i <= 10^80 <= radius_i <= 10^8- Every lit position fits in a 32-bit signed integer.
Examples
Example 1
Input: brightest_position([[1, 3], [8, 2], [4, 2]])
Output: 2
Example 2
Input: brightest_position([[2, 0], [-2, 0]])
Output: -2
Example 3
Input: brightest_position([[14, 6], [2, 4], [11, 1]])
Output: 10
Hints
Hint 1
The range of positions is far too large to count brightness one position at a time, so think about which positions can actually change the brightness.
Hint 2
Brightness only changes where a lamp's lit range begins (it goes up by one) or just after it ends (it goes down by one). Turn each lamp into two such events.
Hint 3
Sort the events and sweep left to right keeping a running total. Apply all events at the same position before comparing, and only update your best answer on a strictly larger total so the smallest position wins ties.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each lamp lights a contiguous range [pos - radius, pos + radius]. The coordinates are huge, so we cannot check every position. Instead, notice that the brightness only changes at two kinds of places for each lamp:
- at
pos - radius, where the lamp starts lighting (brightness increases by 1); - at
pos + radius + 1, the first position no longer lit (brightness decreases by 1).
We record these as a map from position to net change (a sparse difference array). Then we visit the distinct event positions in increasing order, keeping a running brightness total. Merging events at the same position into one net change means we always evaluate the brightness after all simultaneous changes have been applied.
We keep the best brightness seen so far and its position, and update only when the current brightness is strictly greater. Since we sweep from left to right, the first position that reaches the maximum is the smallest one, which gives the required tie-break.
Why it is correct
Between two consecutive event positions nothing changes, so brightness is constant on that stretch. The maximum is therefore reached at the start of some stretch, which is an event position where brightness went up. Within a stretch of equal brightness, the leftmost position is the event position itself, so reporting the first event position that attains the maximum yields the smallest integer position with that brightness. Decreases never create a new maximum, so they do not affect the answer. Because at least one lamp exists, the maximum is at least 1 and best_pos is always set from a real event.
Complexity
- Time:
O(n log n), dominated by sorting at most2ndistinct event positions. - Space:
O(n)for the event map.
Reference solution
from typing import List
def brightest_position(lamps: List[List[int]]) -> int:
events = {}
for pos, radius in lamps:
start = pos - radius
end = pos + radius
events[start] = events.get(start, 0) + 1
events[end + 1] = events.get(end + 1, 0) - 1
brightness = 0
best_count = 0
best_pos = 0
for p in sorted(events):
brightness += events[p]
if brightness > best_count:
best_count = brightness
best_pos = p
return best_pos