Skip to content

Commit 3b3c49b

Browse files
committed
feat: add solutions to lc problem: No.2021
No.2021.Brightest Position on Street
1 parent cd71a68 commit 3b3c49b

File tree

6 files changed

+259
-2
lines changed

6 files changed

+259
-2
lines changed

solution/2000-2099/2021.Brightest Position on Street/README.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,113 @@ Out of all these positions, -1 is the smallest, so return it.
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71+
差分数组 + 排序。
72+
73+
如果用数组实现,空间分配过大。因此可以使用哈希表 + 排序,或者直接使用 TreeMap。
74+
7175
<!-- tabs:start -->
7276

7377
### **Python3**
7478

7579
<!-- 这里可写当前语言的特殊实现逻辑 -->
7680

7781
```python
78-
82+
class Solution:
83+
def brightestPosition(self, lights: List[List[int]]) -> int:
84+
d = defaultdict(int)
85+
for p, r in lights:
86+
d[p - r] += 1
87+
d[p + r + 1] -= 1
88+
s = mx = ans = 0
89+
for k in sorted(d):
90+
s += d[k]
91+
if s > mx:
92+
mx = s
93+
ans = k
94+
return ans
7995
```
8096

8197
### **Java**
8298

8399
<!-- 这里可写当前语言的特殊实现逻辑 -->
84100

85101
```java
102+
class Solution {
103+
public int brightestPosition(int[][] lights) {
104+
TreeMap<Integer, Integer> d = new TreeMap<>();
105+
for (int[] e : lights) {
106+
int l = e[0] - e[1], r = e[0] + e[1];
107+
d.put(l, d.getOrDefault(l, 0) + 1);
108+
d.put(r + 1, d.getOrDefault(r + 1, 0) - 1);
109+
}
110+
int s = 0, mx = 0, ans = 0;
111+
for (Map.Entry<Integer, Integer> e : d.entrySet()) {
112+
s += e.getValue();
113+
if (s > mx) {
114+
mx = s;
115+
ans = e.getKey();
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int brightestPosition(vector<vector<int>>& lights) {
129+
map<int, int> d;
130+
for (auto& e : lights)
131+
{
132+
int l = e[0] - e[1], r = e[0] + e[1];
133+
++d[l];
134+
--d[r + 1];
135+
}
136+
int s = 0, mx = 0, ans = 0;
137+
for (auto& e : d)
138+
{
139+
s += e.second;
140+
if (s > mx)
141+
{
142+
mx = s;
143+
ans = e.first;
144+
}
145+
}
146+
return ans;
147+
}
148+
};
149+
```
86150
151+
### **Go**
152+
153+
```go
154+
func brightestPosition(lights [][]int) int {
155+
d := make(map[int]int)
156+
for _, e := range lights {
157+
l, r := e[0]-e[1], e[0]+e[1]
158+
d[l] += 1
159+
d[r+1] -= 1
160+
}
161+
162+
var keys []int
163+
for k := range d {
164+
keys = append(keys, k)
165+
}
166+
sort.Ints(keys)
167+
168+
s, mx, ans := 0, 0, 0
169+
for _, k := range keys {
170+
s += d[k]
171+
if s > mx {
172+
mx = s
173+
ans = k
174+
}
175+
}
176+
return ans
177+
}
87178
```
88179

89180
### **...**

solution/2000-2099/2021.Brightest Position on Street/README_EN.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,100 @@ Out of all these positions, -1 is the smallest, so return it.
6969
### **Python3**
7070

7171
```python
72-
72+
class Solution:
73+
def brightestPosition(self, lights: List[List[int]]) -> int:
74+
d = defaultdict(int)
75+
for p, r in lights:
76+
d[p - r] += 1
77+
d[p + r + 1] -= 1
78+
s = mx = ans = 0
79+
for k in sorted(d):
80+
s += d[k]
81+
if s > mx:
82+
mx = s
83+
ans = k
84+
return ans
7385
```
7486

7587
### **Java**
7688

7789
```java
90+
class Solution {
91+
public int brightestPosition(int[][] lights) {
92+
TreeMap<Integer, Integer> d = new TreeMap<>();
93+
for (int[] e : lights) {
94+
int l = e[0] - e[1], r = e[0] + e[1];
95+
d.put(l, d.getOrDefault(l, 0) + 1);
96+
d.put(r + 1, d.getOrDefault(r + 1, 0) - 1);
97+
}
98+
int s = 0, mx = 0, ans = 0;
99+
for (Map.Entry<Integer, Integer> e : d.entrySet()) {
100+
s += e.getValue();
101+
if (s > mx) {
102+
mx = s;
103+
ans = e.getKey();
104+
}
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int brightestPosition(vector<vector<int>>& lights) {
117+
map<int, int> d;
118+
for (auto& e : lights)
119+
{
120+
int l = e[0] - e[1], r = e[0] + e[1];
121+
++d[l];
122+
--d[r + 1];
123+
}
124+
int s = 0, mx = 0, ans = 0;
125+
for (auto& e : d)
126+
{
127+
s += e.second;
128+
if (s > mx)
129+
{
130+
mx = s;
131+
ans = e.first;
132+
}
133+
}
134+
return ans;
135+
}
136+
};
137+
```
78138
139+
### **Go**
140+
141+
```go
142+
func brightestPosition(lights [][]int) int {
143+
d := make(map[int]int)
144+
for _, e := range lights {
145+
l, r := e[0]-e[1], e[0]+e[1]
146+
d[l] += 1
147+
d[r+1] -= 1
148+
}
149+
150+
var keys []int
151+
for k := range d {
152+
keys = append(keys, k)
153+
}
154+
sort.Ints(keys)
155+
156+
s, mx, ans := 0, 0, 0
157+
for _, k := range keys {
158+
s += d[k]
159+
if s > mx {
160+
mx = s
161+
ans = k
162+
}
163+
}
164+
return ans
165+
}
79166
```
80167

81168
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int brightestPosition(vector<vector<int>>& lights) {
4+
map<int, int> d;
5+
for (auto& e : lights)
6+
{
7+
int l = e[0] - e[1], r = e[0] + e[1];
8+
++d[l];
9+
--d[r + 1];
10+
}
11+
int s = 0, mx = 0, ans = 0;
12+
for (auto& e : d)
13+
{
14+
s += e.second;
15+
if (s > mx)
16+
{
17+
mx = s;
18+
ans = e.first;
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func brightestPosition(lights [][]int) int {
2+
d := make(map[int]int)
3+
for _, e := range lights {
4+
l, r := e[0]-e[1], e[0]+e[1]
5+
d[l] += 1
6+
d[r+1] -= 1
7+
}
8+
9+
var keys []int
10+
for k := range d {
11+
keys = append(keys, k)
12+
}
13+
sort.Ints(keys)
14+
15+
s, mx, ans := 0, 0, 0
16+
for _, k := range keys {
17+
s += d[k]
18+
if s > mx {
19+
mx = s
20+
ans = k
21+
}
22+
}
23+
return ans
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int brightestPosition(int[][] lights) {
3+
TreeMap<Integer, Integer> d = new TreeMap<>();
4+
for (int[] e : lights) {
5+
int l = e[0] - e[1], r = e[0] + e[1];
6+
d.put(l, d.getOrDefault(l, 0) + 1);
7+
d.put(r + 1, d.getOrDefault(r + 1, 0) - 1);
8+
}
9+
int s = 0, mx = 0, ans = 0;
10+
for (Map.Entry<Integer, Integer> e : d.entrySet()) {
11+
s += e.getValue();
12+
if (s > mx) {
13+
mx = s;
14+
ans = e.getKey();
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def brightestPosition(self, lights: List[List[int]]) -> int:
3+
d = defaultdict(int)
4+
for p, r in lights:
5+
d[p - r] += 1
6+
d[p + r + 1] -= 1
7+
s = mx = ans = 0
8+
for k in sorted(d):
9+
s += d[k]
10+
if s > mx:
11+
mx = s
12+
ans = k
13+
return ans

0 commit comments

Comments
 (0)