Imagine a street with several street lights. Each light has a position and a range. The "brightness" at any point on the street is the number of lights that cover that point. Given a list of lights, you need to find the smallest coordinate that has the maximum brightness.
This is a standard interval-based problem frequently seen in Amazon and TikTok interviews. it's a great test of your ability to handle coordinate-based data and identify the "Sweep Line" algorithm. It evaluates how you manage events (start and end of light ranges) and how you find the peak of a cumulative distribution.
This problem uses the Sweep Line pattern combined with a Prefix Sum (Difference Array) approach. For each light at position with range , it covers the interval . You create "events": a +1 at and a -1 at . Sort these events by coordinate. Then, iterate through the sorted events, maintaining a running sum (the current brightness). The position where the sum is at its maximum is your answer.
Light 1: Pos 5, Range 2 -> Covers [3, 7] Light 2: Pos 6, Range 1 -> Covers [5, 7] Events:
Sweep:
A frequent mistake is not handling the end of the interval correctly—specifically, the boundary condition of whether the endpoint is inclusive or exclusive. Another error is using a naive approach (marking every integer in the range), which fails if the ranges are large. The sweep-line approach is regardless of the range size.
Whenever you see a problem involving overlapping intervals and finding a point of maximum overlap, think "Sweep Line." Sorting the start and end points as events is a powerful technique for these scenarios.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Number of Flowers in Full Bloom | Hard | Solve | |
| Minimum Absolute Sum Difference | Medium | Solve | |
| Describe the Painting | Medium | Solve | |
| Minimum Operations to Make All Array Elements Equal | Medium | Solve | |
| Rearrange Array to Maximize Prefix Score | Medium | Solve |