Skip to content

Commit 19ec8d9

Browse files
committed
feat: add solutions to lc problem: No.2237
No.2237.Count Positions on Street With Required Brightness
1 parent cb758d9 commit 19ec8d9

File tree

6 files changed

+268
-2
lines changed

6 files changed

+268
-2
lines changed

Diff for: solution/2200-2299/2237.Count Positions on Street With Required Brightness/README.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,116 @@ Positions 0, 1, 2, and 4 meet the requirement so we return 4.
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:差分数组**
64+
65+
时间复杂度 O(n)。
66+
6367
<!-- tabs:start -->
6468

6569
### **Python3**
6670

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

6973
```python
70-
74+
class Solution:
75+
def meetRequirement(self, n: int, lights: List[List[int]], requirement: List[int]) -> int:
76+
d = [0] * 100010
77+
for p, r in lights:
78+
i, j = max(0, p - r), min(n - 1, p + r)
79+
d[i] += 1
80+
d[j + 1] -= 1
81+
s = ans = 0
82+
for i, r in enumerate(requirement):
83+
s += d[i]
84+
if s >= r:
85+
ans += 1
86+
return ans
7187
```
7288

7389
### **Java**
7490

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

7793
```java
94+
class Solution {
95+
public int meetRequirement(int n, int[][] lights, int[] requirement) {
96+
int[] d = new int[100010];
97+
for (int[] e : lights) {
98+
int i = Math.max(0, e[0] - e[1]);
99+
int j = Math.min(n - 1, e[0] + e[1]);
100+
++d[i];
101+
--d[j + 1];
102+
}
103+
int s = 0;
104+
int ans = 0;
105+
for (int i = 0; i < n; ++i) {
106+
s += d[i];
107+
if (s >= requirement[i]) {
108+
++ans;
109+
}
110+
}
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
122+
vector<int> d(100010);
123+
for (auto& e : lights)
124+
{
125+
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
126+
++d[i];
127+
--d[j + 1];
128+
}
129+
int s = 0, ans = 0;
130+
for (int i = 0; i < n; ++i)
131+
{
132+
s += d[i];
133+
if (s >= requirement[i]) ++ans;
134+
}
135+
return ans;
136+
}
137+
};
138+
```
78139
140+
### **Go**
141+
142+
```go
143+
func meetRequirement(n int, lights [][]int, requirement []int) int {
144+
d := make([]int, 100010)
145+
for _, e := range lights {
146+
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
147+
d[i]++
148+
d[j+1]--
149+
}
150+
var s, ans int
151+
for i, r := range requirement {
152+
s += d[i]
153+
if s >= r {
154+
ans++
155+
}
156+
}
157+
return ans
158+
}
159+
160+
func max(a, b int) int {
161+
if a > b {
162+
return a
163+
}
164+
return b
165+
}
166+
167+
func min(a, b int) int {
168+
if a < b {
169+
return a
170+
}
171+
return b
172+
}
79173
```
80174

81175
### **TypeScript**

Diff for: solution/2200-2299/2237.Count Positions on Street With Required Brightness/README_EN.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,103 @@ Positions 0, 1, 2, and 4 meet the requirement so we return 4.
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def meetRequirement(self, n: int, lights: List[List[int]], requirement: List[int]) -> int:
66+
d = [0] * 100010
67+
for p, r in lights:
68+
i, j = max(0, p - r), min(n - 1, p + r)
69+
d[i] += 1
70+
d[j + 1] -= 1
71+
s = ans = 0
72+
for i, r in enumerate(requirement):
73+
s += d[i]
74+
if s >= r:
75+
ans += 1
76+
return ans
6577
```
6678

6779
### **Java**
6880

6981
```java
82+
class Solution {
83+
public int meetRequirement(int n, int[][] lights, int[] requirement) {
84+
int[] d = new int[100010];
85+
for (int[] e : lights) {
86+
int i = Math.max(0, e[0] - e[1]);
87+
int j = Math.min(n - 1, e[0] + e[1]);
88+
++d[i];
89+
--d[j + 1];
90+
}
91+
int s = 0;
92+
int ans = 0;
93+
for (int i = 0; i < n; ++i) {
94+
s += d[i];
95+
if (s >= requirement[i]) {
96+
++ans;
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
110+
vector<int> d(100010);
111+
for (auto& e : lights)
112+
{
113+
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
114+
++d[i];
115+
--d[j + 1];
116+
}
117+
int s = 0, ans = 0;
118+
for (int i = 0; i < n; ++i)
119+
{
120+
s += d[i];
121+
if (s >= requirement[i]) ++ans;
122+
}
123+
return ans;
124+
}
125+
};
126+
```
70127
128+
### **Go**
129+
130+
```go
131+
func meetRequirement(n int, lights [][]int, requirement []int) int {
132+
d := make([]int, 100010)
133+
for _, e := range lights {
134+
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
135+
d[i]++
136+
d[j+1]--
137+
}
138+
var s, ans int
139+
for i, r := range requirement {
140+
s += d[i]
141+
if s >= r {
142+
ans++
143+
}
144+
}
145+
return ans
146+
}
147+
148+
func max(a, b int) int {
149+
if a > b {
150+
return a
151+
}
152+
return b
153+
}
154+
155+
func min(a, b int) int {
156+
if a < b {
157+
return a
158+
}
159+
return b
160+
}
71161
```
72162

73163
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
4+
vector<int> d(100010);
5+
for (auto& e : lights)
6+
{
7+
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
8+
++d[i];
9+
--d[j + 1];
10+
}
11+
int s = 0, ans = 0;
12+
for (int i = 0; i < n; ++i)
13+
{
14+
s += d[i];
15+
if (s >= requirement[i]) ++ans;
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func meetRequirement(n int, lights [][]int, requirement []int) int {
2+
d := make([]int, 100010)
3+
for _, e := range lights {
4+
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
5+
d[i]++
6+
d[j+1]--
7+
}
8+
var s, ans int
9+
for i, r := range requirement {
10+
s += d[i]
11+
if s >= r {
12+
ans++
13+
}
14+
}
15+
return ans
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int meetRequirement(int n, int[][] lights, int[] requirement) {
3+
int[] d = new int[100010];
4+
for (int[] e : lights) {
5+
int i = Math.max(0, e[0] - e[1]);
6+
int j = Math.min(n - 1, e[0] + e[1]);
7+
++d[i];
8+
--d[j + 1];
9+
}
10+
int s = 0;
11+
int ans = 0;
12+
for (int i = 0; i < n; ++i) {
13+
s += d[i];
14+
if (s >= requirement[i]) {
15+
++ans;
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def meetRequirement(self, n: int, lights: List[List[int]], requirement: List[int]) -> int:
3+
d = [0] * 100010
4+
for p, r in lights:
5+
i, j = max(0, p - r), min(n - 1, p + r)
6+
d[i] += 1
7+
d[j + 1] -= 1
8+
s = ans = 0
9+
for i, r in enumerate(requirement):
10+
s += d[i]
11+
if s >= r:
12+
ans += 1
13+
return ans

0 commit comments

Comments
 (0)