Skip to content

Commit 2d3ebb7

Browse files
authored
Merge pull request #2636 from ananyag309/main
added lc soln of 2021
2 parents 65890ce + 60ead2f commit 2d3ebb7

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: brightest-position-on-street
3+
title: Brightest Position on Street
4+
sidebar_label: 2021 Brightest Position on Street
5+
tags:
6+
- Array
7+
- Prefix Sum
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Brightest Position on Street problem on LeetCode."
11+
sidebar_position: 2021
12+
---
13+
14+
## Problem Description
15+
16+
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] = [positioni, rangei]` indicates that there is a street lamp at `positioni` that lights up the area from `[positioni - rangei, positioni + rangei]` (inclusive).
17+
18+
The brightness of a position `p` is defined as the number of street lamp that light up the position `p`.
19+
20+
Given `lights`, return the brightest position on the street. If there are multiple brightest positions, return the smallest one.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
```
27+
Input: lights = [[-3,2],[1,2],[3,3]]
28+
Output: -1
29+
Explanation:
30+
The first street lamp lights up the area from [(-3) - 2, (-3) + 2] = [-5, -1].
31+
The second street lamp lights up the area from [1 - 2, 1 + 2] = [-1, 3].
32+
The third street lamp lights up the area from [3 - 3, 3 + 3] = [0, 6].
33+
34+
Position -1 has a brightness of 2, illuminated by the first and second street light.
35+
Positions 0, 1, 2, and 3 have a brightness of 2, illuminated by the second and third street light.
36+
Out of all these positions, -1 is the smallest, so return it.
37+
38+
```
39+
40+
**Example 2:**
41+
42+
```
43+
Input: lights = [[1,0],[0,1]]
44+
Output: 1
45+
Explanation:
46+
The first street lamp lights up the area from [1 - 0, 1 + 0] = [1, 1].
47+
The second street lamp lights up the area from [0 - 1, 0 + 1] = [-1, 1].
48+
49+
Position 1 has a brightness of 2, illuminated by the first and second street light.
50+
Return 1 because it is the brightest position on the street.
51+
52+
```
53+
54+
**Example 3:**
55+
56+
```
57+
Input: lights = [[1,2]]
58+
Output: -1
59+
Explanation:
60+
The first street lamp lights up the area from [1 - 2, 1 + 2] = [-1, 3].
61+
62+
Positions -1, 0, 1, 2, and 3 have a brightness of 1, illuminated by the first street light.
63+
Out of all these positions, -1 is the smallest, so return it.
64+
65+
```
66+
67+
68+
### Constraints
69+
70+
- `1 <= lights.length <= 10^5`
71+
- `lights[i].length == 2`
72+
- `-10^8 <= positioni <= 10^8`
73+
- `0 <= rangei <= 10^8`
74+
75+
### Approach
76+
77+
We can solve this problem using a prefix sum approach to handle the range updates efficiently. We will mark the start and end of each light's range and then use a sweep line algorithm to determine the brightest position.
78+
79+
### Complexity
80+
81+
- **Time complexity**: $O(n \log n)$ due to sorting the events.
82+
- **Space complexity**: $O(n)$ for storing the events.
83+
84+
#### C++
85+
86+
```cpp
87+
88+
class Solution {
89+
public:
90+
int brightestPosition(vector<vector<int>>& lights) {
91+
map<int, int> events;
92+
93+
for (const auto& light : lights) {
94+
int start = light[0] - light[1];
95+
int end = light[0] + light[1];
96+
events[start]++;
97+
events[end + 1]--;
98+
}
99+
100+
int maxBrightness = 0, currentBrightness = 0, brightestPos = 0;
101+
102+
for (const auto& event : events) {
103+
currentBrightness += event.second;
104+
if (currentBrightness > maxBrightness) {
105+
maxBrightness = currentBrightness;
106+
brightestPos = event.first;
107+
}
108+
}
109+
110+
return brightestPos;
111+
}
112+
};
113+
```

0 commit comments

Comments
 (0)