Brightest Position on Street - Problem

Imagine you're a city planner tasked with finding the brightest spot on a perfectly straight street! ๐ŸŒŸ

The street is represented by a number line, and you have several street lamps positioned along it. Each lamp is described by a 2D array lights[i] = [position, range], where:

  • position - where the lamp is located on the street
  • range - how far the light extends in both directions

A lamp at position p with range r illuminates the area from [p-r, p+r] (inclusive).

The brightness of any position is simply the number of lamps that light up that position. Your goal is to find the position with maximum brightness. If multiple positions tie for brightest, return the smallest position.

Example: If we have lamps at [[1,2], [3,1]], the first lamp lights [-1,3] and the second lights [2,4]. Position 2 and 3 both have brightness 2 (lit by both lamps), so we return 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: lights = [[1,2], [3,1]]
โ€บ Output: 2
๐Ÿ’ก Note: Lamp 1 at position 1 with range 2 covers [-1, 3]. Lamp 2 at position 3 with range 1 covers [2, 4]. Positions 2 and 3 both have brightness 2, so we return the smallest: 2.
example_2.py โ€” Single Lamp
$ Input: lights = [[1,0]]
โ€บ Output: 1
๐Ÿ’ก Note: Only one lamp at position 1 with range 0, so it only lights up position 1. The brightest (and only lit) position is 1.
example_3.py โ€” No Overlap
$ Input: lights = [[1,1], [5,1]]
โ€บ Output: 0
๐Ÿ’ก Note: Lamp 1 covers [0, 2] and Lamp 2 covers [4, 6]. Both regions have brightness 1. The smallest position with maximum brightness is 0.

Constraints

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

Visualization

Tap to expand
Street Lamp Brightness VisualizationLamp 1pos=3, range=2Lamp 2pos=5, range=1BrightestPosition 4Position 1: brightness 1Position 4: brightness 2 โญPosition 6: brightness 1
Understanding the Visualization
1
Place Lamps
Each lamp creates a circle of light with specified range
2
Find Overlaps
Areas where multiple circles overlap are brighter
3
Track Changes
Brightness only changes at lamp boundaries
4
Find Maximum
The brightest spot has the most overlapping lights
Key Takeaway
๐ŸŽฏ Key Insight: Brightness only changes at lamp boundaries, so we only need to check these critical positions instead of every possible location.
Asked in
Google 28 Amazon 15 Meta 12 Microsoft 8
38.4K Views
Medium Frequency
~25 min Avg. Time
1.2K 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