Brightest Position on Street

Medium

Topics
ArrayOrdered SetPrefix SumSortingLine Sweep

A street is the number line. You are given lights, where lights[i] = [position, range] describes a street lamp standing at position that lights up every integer point in the closed interval [position - range, position + range].

The brightness of a point is the number of lamps lighting it up.

Return the brightest position on the street. If several positions tie for brightest, return the smallest one.

The judge names this argument grid, since it feeds every matrix problem through the same reader. It is the lights array above, one row per lamp.

Example 1

-56[-5,-1][-1,3][0,6]answer
lamps lighting the answer position
Each lane is one lamp’s coverage, [position - range, position + range], including both ends. The brightness of a point is how many lanes cover it. 2 lamps overlap at position -1, which is the most anywhere on the street, and it is the smallest of the 2 positions that reach it.
Each lane is one interval, drawn on a shared axis so overlaps are visible.
Input:  lights = [[-3,2],[1,2],[3,3]]
Output: -1
Explanation: The lamps cover [-5,-1], [-1,3] and [0,6]. Brightness peaks at 2, first reached at position -1, which the first two lamps share.

Example 2

-11[1,1][-1,1]answer
lamps lighting the answer position
The brightness of a point is how many lanes cover it. 2 lamps overlap at position 1, which is the most anywhere on the street.
Input:  lights = [[1,0],[0,1]]
Output: 1
Explanation: The lamps cover [1,1] and [-1,1]. Only position 1 has brightness 2.

Example 3

-13[-1,3]answer
lamps lighting the answer position
Input:  lights = [[1,2]]
Output: -1
Explanation: A single lamp covers [-1,3] evenly, so every point in it has brightness 1 and the smallest is -1.

Constraints

  • 1 <= lights.length <= 10^5
  • lights[i].length == 2
  • -10^8 <= position <= 10^8
  • 0 <= range <= 10^8
Editorial — intuition, approach, complexity and pitfalls

Intuition

The positions themselves are unbounded, so walking the number line is not an option. What is bounded is the number of places where the brightness can change: it only ever goes up where some lamp’s coverage begins, and down just past where one ends.

Between two consecutive such points the brightness is flat. That has a direct consequence for the tie-break: the smallest position holding the maximum is always a point where coverage begins, so only those positions need examining — at most one per lamp.

Note the interval is closed on both sides, so a lamp’s influence ends after position + range, not at it. Getting that boundary wrong is what makes example 2 come out as 0 instead of 1.

Approach

  1. Turn each lamp into two events: +1 at position - range, and -1 at position + range + 1.
  2. Sort the events by coordinate. Where a start and an end share a coordinate, the start must be applied first, since the point is lit by both lamps.
  3. Sweep through the events in order, keeping a running brightness. After applying every event at the current coordinate, compare that brightness to the best seen.
  4. Record the coordinate only when the brightness strictly beats the best so far. Keeping the first such coordinate is what delivers the smallest position on a tie.

Walkthrough

lights = [[-3,2],[1,2],[3,3]]

coverage   [-5, -1]   [-1, 3]   [0, 6]

events sorted by coordinate
  -5 : +1   running 1   best 1 at -5
  -1 : +1   running 2   best 2 at -1
   0 : -1 (from the first lamp, which ended at -1)
       +1 (the third lamp starts)
       running 2       not strictly better, so -1 stands
   4 : -1   running 1
   7 : -1   running 0

answer -1

Complexity

  • Time — O(n log n): two events per lamp, dominated by sorting them.
  • Space — O(n): the event list.

Common Pitfalls

  • Ending a lamp’s contribution at position + range instead of one past it. The interval is closed, so the last lit point would lose a lamp it should have.
  • Updating the recorded position on a tie (>= rather than >). That returns the largest brightest position, not the smallest.
  • Comparing the running total mid-coordinate, before every event at that coordinate has been applied. A start and an end that share a coordinate would then be read as a dip that does not exist.

Full Solution — all languages

class Solution:
    def brightestPosition(self, grid):
        events = []
        for position, reach in grid:
            events.append((position - reach, 1))
            events.append((position + reach + 1, -1))
        events.sort(key=lambda e: (e[0], -e[1]))

        best_count = 0
        best_pos = 0
        running = 0
        i = 0
        while i < len(events):
            x = events[i][0]
            while i < len(events) and events[i][0] == x:
                running += events[i][1]
                i += 1
            if running > best_count:
                best_count = running
                best_pos = x
        return best_pos
SensAI
Beta
Tap the mic to stop voice mode