Brightest Position on Street - Problem

A perfectly straight street is represented by a number line. The street has street lamp(s) on it and is represented by a 2D integer array lights. Each lights[i] = [position_i, range_i] indicates that there is a street lamp at position position_i that lights up the area from [position_i - range_i, position_i + range_i] (inclusive).

The brightness of a position p is defined as the number of street lamps that light up the position p.

Given lights, return the brightest position on the street. If there are multiple brightest positions, return the smallest one.

Input & Output

Example 1 — Basic Two Lamps
$ Input: lights = [[1,1],[3,2]]
Output: 1
💡 Note: Lamp 1 at position 1 with range 1 covers [0,2]. Lamp 2 at position 3 with range 2 covers [1,5]. Position 1 and 2 both have brightness 2, but we return the smaller position 1.
Example 2 — Single Lamp
$ Input: lights = [[2,3]]
Output: -1
💡 Note: Single lamp at position 2 with range 3 covers [-1,5]. All positions have brightness 1, so return the smallest position -1.
Example 3 — Multiple Overlaps
$ Input: lights = [[1,0],[0,1]]
Output: 1
💡 Note: Lamp 1 at position 1 with range 0 covers [1,1]. Lamp 2 at position 0 with range 1 covers [-1,1]. Position 1 has brightness 2 (both lamps overlap), while other positions have brightness 1, so return position 1.

Constraints

  • 1 ≤ lights.length ≤ 105
  • lights[i].length == 2
  • -108 ≤ positioni ≤ 108
  • 0 ≤ rangei ≤ 108

Visualization

Tap to expand
Brightest Position on Street INPUT Street (Number Line) -1 0 1 2 3 4 Lamp 1 Lamp 2 lights array: [1, 1] [3, 2] pos=1, range=1 covers: [0, 2] pos=3, range=2 covers: [1, 5] Overlap at positions 1 and 2 Both lamps cover [1, 2] ALGORITHM STEPS 1 Convert to Events Mark start (+1) and end (-1) (0,+1) (1,+1) (3,-1) (6,-1) Lamp1: start=0, end=3 Lamp2: start=1, end=6 2 Sort Events By position, starts before ends Sorted: (0,+1)(1,+1)(3,-1)(6,-1) 3 Sweep Line Track brightness at each point pos=0: bright=1 pos=1: bright=2 [MAX] pos=3: bright=1 pos=6: bright=0 4 Find Maximum Return smallest brightest pos Max brightness=2 at position 1 FINAL RESULT Brightness at each position: 1 0 2 1 2 2 1 3 Position 1 and 2 both have brightness = 2 Return smallest: 1 Output: 1 Key Insight: Use a sweep line algorithm with events. Convert each lamp's range [pos-range, pos+range] into two events: +1 at start and -1 after end. Sort events and sweep through, tracking cumulative brightness. This achieves O(n log n) time complexity vs O(n * range) brute force approach. TutorialsPoint - Brightest Position on Street | Sweep Line Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen