Brightest Position on Street
Medium
Topics
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
[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.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
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
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
- Turn each lamp into two events:
+1atposition - range, and-1atposition + range + 1. - 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.
- 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.
- 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 -1Complexity
- 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 + rangeinstead 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_posimport java.util.*;
class Solution {
public int brightestPosition(int[][] grid) {
List<int[]> events = new ArrayList<>();
for (int[] l : grid) {
events.add(new int[]{l[0] - l[1], 1});
events.add(new int[]{l[0] + l[1] + 1, -1});
}
events.sort((a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(b[1], a[1]));
int best = 0, bestPos = 0, running = 0, i = 0;
while (i < events.size()) {
int x = events.get(i)[0];
while (i < events.size() && events.get(i)[0] == x) {
running += events.get(i)[1];
i++;
}
if (running > best) {
best = running;
bestPos = x;
}
}
return bestPos;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int brightestPosition(vector<vector<int>>& grid) {
vector<pair<int, int>> events;
for (auto& l : grid) {
events.push_back({l[0] - l[1], 1});
events.push_back({l[0] + l[1] + 1, -1});
}
sort(events.begin(), events.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
if (a.first != b.first) return a.first < b.first; return a.second > b.second;
})
;
int best = 0, bestPos = 0, running = 0;
size_t i = 0;
while (i < events.size()) {
int x = events[i].first;
while (i < events.size() && events[i].first == x) {
running += events[i].second;
i++;
}
if (running > best) {
best = running;
bestPos = x;
}
}
return bestPos;
}
};/**
* @param {number[][]} grid
* @return {number}
*/ var brightestPosition = function(grid) {
const events = [];
for (const l of grid) {
events.push([l[0] - l[1], 1]);
events.push([l[0] + l[1] + 1, -1]);
}
events.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
let best = 0, bestPos = 0, running = 0, i = 0;
while (i < events.length) {
const x = events[i][0];
while (i < events.length && events[i][0] === x) {
running += events[i][1];
i++;
}
if (running > best) {
best = running;
bestPos = x;
}
}
return bestPos;
};