There is a street represented as a number line. You are given 2D integer array lights
where lights[i] = [positioni, rangei]
indicates that there is a light at position positioni
that lights the area from positioni - rangei
to positioni + rangei
(inclusive).
You want to find the brightest position on the street. A position on the street is bright if the number of lights that light up the position is the greatest.
Return the brightest position on the street. If there are multiple brightest positions, return the smallest one.
Example 1:
Input: lights = [[-3,2],[1,2],[3,3]] Output: -1 Explanation: The first light lights the area from -3 - 2 = -5 to -3 + 2 = -1. The second light lights the area from 1 - 2 = -1 to 1 + 2 = 3. The third light lights the area from 3 - 3 = 0 to 3 + 3 = 6. The position -1 has the greatest number of lights shining on it, which is 2.
Example 2:
Input: lights = [[1,0],[0,1]] Output: 1 Explanation: The first light lights the area from 1 - 0 = 1 to 1 + 0 = 1. The second light lights the area from 0 - 1 = -1 to 0 + 1 = 1. The position 1 has the greatest number of lights shining on it, which is 2.
Constraints:
1 <= lights.length <= 105
lights[i].length == 2
-108 <= positioni <= 108
0 <= rangei <= 108
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the brightest spot on a street using a brute-force method, imagine walking down the street and, for every single position, checking the brightness caused by all the lights. We calculate the total brightness for each possible position on the street.
Here's how the algorithm would work step-by-step:
def brightest_position_brute_force(lights):
max_position = -1
max_brightness = 0
# Iterate through all possible positions
for current_position in range(1, 101):
total_brightness = 0
# Iterate through all lights to calculate total brightness at position
for light_position, light_range, light_brightness in lights:
if current_position >= light_position - light_range and \
current_position <= light_position + light_range:
total_brightness += light_brightness
# Find the position with the maximum brightness
if total_brightness > max_brightness:
# Update the max brightness and position
max_brightness = total_brightness
max_position = current_position
return max_position
The core idea is to treat each house's location and brightness as key moments where the total brightness on the street changes. We'll efficiently track how the overall brightness increases and decreases at these locations to find the point with the maximum brightness. We accomplish this without individually checking every house or position.
Here's how the algorithm would work step-by-step:
def brightest_position(lights):
brightness_changes = []
for position, range_value, brightness in lights:
brightness_changes.append((position, brightness))
brightness_changes.append((position + range_value, -brightness))
# Sorting ensures events are processed in the correct order.
brightness_changes.sort()
current_brightness = 0
max_brightness = 0
brightest_position_so_far = 0
for position, brightness_change in brightness_changes:
current_brightness += brightness_change
# Keep track of the position with the maximum brightness seen so far
if current_brightness > max_brightness:
max_brightness = current_brightness
brightest_position_so_far = position
# Return the position with the highest brightness
return brightest_position_so_far
Case | How to Handle |
---|---|
Empty positions or strengths array | Return -1 or throw an exception as there are no lights to consider. |
positions and strengths arrays have different lengths | Return -1 or throw an IllegalArgumentException to signal an invalid input. |
k is zero | The brightness at any position is just the strength of the light at that position, so return the position with the max strength. |
All positions are the same | The position with the maximum strength among the duplicated ones is the result. |
All strengths are the same | The position among the positions with the smallest values is the desired result. |
positions are very large numbers, potentially causing integer overflow during calculation of p - k and p + k | Use long data type for intermediate calculations to prevent potential overflow issues. |
Large input size (N positions), causing performance issues | Optimize the brightness calculation using a prefix sum or sliding window approach for better time complexity. |
Negative values in positions and/or strengths | The solution should correctly handle negative positions and strengths as the brightness can be negative or the positions may be on the negative side of the number line. |