Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04070a3

Browse files
feat: add solutions to lc problem: No.2237 (doocs#3938)
No.2237.Count Positions on Street With Required Brightness
1 parent 16116c4 commit 04070a3

File tree

8 files changed

+121
-33
lines changed

8 files changed

+121
-33
lines changed

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ tags:
7777

7878
### 方法一:差分数组
7979

80-
时间复杂度 $O(n)$。
80+
对一段连续的区间 $[i, j]$ 同时加上一个值 $v,ドル可以通过差分数组来实现。
81+
82+
我们定义一个长度为 $n + 1$ 的数组 $\textit{d},ドル接下来对于每个路灯,我们计算出它的左边界 $i = \max(0, p - r)$ 和右边界 $j = \min(n - 1, p + r),ドル然后将 $\textit{d}[i]$ 加上 1ドル,ドル将 $\textit{d}[j + 1]$ 减去 1ドル$。
83+
84+
然后,我们对 $\textit{d}$ 进行前缀和运算,对于每个位置 $i,ドル如果 $\textit{d}[i]$ 的前缀和大于等于 $\textit{requirement}[i],ドル则说明该位置满足要求,将答案加一。
85+
86+
最后返回答案即可。
87+
88+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为路灯数量。
8189

8290
<!-- tabs:start -->
8391

@@ -88,7 +96,7 @@ class Solution:
8896
def meetRequirement(
8997
self, n: int, lights: List[List[int]], requirement: List[int]
9098
) -> int:
91-
d = [0] * 100010
99+
d = [0] * (n +1)
92100
for p, r in lights:
93101
i, j = max(0, p - r), min(n - 1, p + r)
94102
d[i] += 1
@@ -101,7 +109,7 @@ class Solution:
101109
```java
102110
class Solution {
103111
public int meetRequirement(int n, int[][] lights, int[] requirement) {
104-
int[] d = new int[100010];
112+
int[] d = new int[n +1];
105113
for (int[] e : lights) {
106114
int i = Math.max(0, e[0] - e[1]);
107115
int j = Math.min(n - 1, e[0] + e[1]);
@@ -127,16 +135,18 @@ class Solution {
127135
class Solution {
128136
public:
129137
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
130-
vector<int> d(100010);
131-
for (auto& e : lights) {
138+
vector<int> d(n + 1);
139+
for (const auto& e : lights) {
132140
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
133141
++d[i];
134142
--d[j + 1];
135143
}
136144
int s = 0, ans = 0;
137145
for (int i = 0; i < n; ++i) {
138146
s += d[i];
139-
if (s >= requirement[i]) ++ans;
147+
if (s >= requirement[i]) {
148+
++ans;
149+
}
140150
}
141151
return ans;
142152
}
@@ -146,21 +156,42 @@ public:
146156
#### Go
147157
148158
```go
149-
func meetRequirement(n int, lights [][]int, requirement []int) int {
150-
d := make([]int, 100010)
159+
func meetRequirement(n int, lights [][]int, requirement []int) (ans int) {
160+
d := make([]int, n+1)
151161
for _, e := range lights {
152162
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
153163
d[i]++
154164
d[j+1]--
155165
}
156-
var s, ans int
166+
s := 0
157167
for i, r := range requirement {
158168
s += d[i]
159169
if s >= r {
160170
ans++
161171
}
162172
}
163-
return ans
173+
return
174+
}
175+
```
176+
177+
#### TypeScript
178+
179+
```ts
180+
function meetRequirement(n: number, lights: number[][], requirement: number[]): number {
181+
const d: number[] = Array(n + 1).fill(0);
182+
for (const [p, r] of lights) {
183+
const [i, j] = [Math.max(0, p - r), Math.min(n - 1, p + r)];
184+
++d[i];
185+
--d[j + 1];
186+
}
187+
let [ans, s] = [0, 0];
188+
for (let i = 0; i < n; ++i) {
189+
s += d[i];
190+
if (s >= requirement[i]) {
191+
++ans;
192+
}
193+
}
194+
return ans;
164195
}
165196
```
166197

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ Positions 0, 1, 2, and 4 meet the requirement so we return 4.
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Difference Array
77+
78+
To add a value $v$ to a continuous interval $[i, j]$ simultaneously, we can use a difference array.
79+
80+
We define an array $\textit{d}$ of length $n + 1$. For each streetlight, we calculate its left boundary $i = \max(0, p - r)$ and right boundary $j = \min(n - 1, p + r),ドル then add 1ドル$ to $\textit{d}[i]$ and subtract 1ドル$ from $\textit{d}[j + 1]$.
81+
82+
Next, we perform a prefix sum operation on $\textit{d}$. For each position $i,ドル if the prefix sum of $\textit{d}[i]$ is greater than or equal to $\textit{requirement}[i],ドル it means that the position meets the requirement, and we increment the answer by one.
83+
84+
Finally, return the answer.
85+
86+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the number of streetlights.
7787

7888
<!-- tabs:start -->
7989

@@ -84,7 +94,7 @@ class Solution:
8494
def meetRequirement(
8595
self, n: int, lights: List[List[int]], requirement: List[int]
8696
) -> int:
87-
d = [0] * 100010
97+
d = [0] * (n +1)
8898
for p, r in lights:
8999
i, j = max(0, p - r), min(n - 1, p + r)
90100
d[i] += 1
@@ -97,7 +107,7 @@ class Solution:
97107
```java
98108
class Solution {
99109
public int meetRequirement(int n, int[][] lights, int[] requirement) {
100-
int[] d = new int[100010];
110+
int[] d = new int[n +1];
101111
for (int[] e : lights) {
102112
int i = Math.max(0, e[0] - e[1]);
103113
int j = Math.min(n - 1, e[0] + e[1]);
@@ -123,16 +133,18 @@ class Solution {
123133
class Solution {
124134
public:
125135
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
126-
vector<int> d(100010);
127-
for (auto& e : lights) {
136+
vector<int> d(n + 1);
137+
for (const auto& e : lights) {
128138
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
129139
++d[i];
130140
--d[j + 1];
131141
}
132142
int s = 0, ans = 0;
133143
for (int i = 0; i < n; ++i) {
134144
s += d[i];
135-
if (s >= requirement[i]) ++ans;
145+
if (s >= requirement[i]) {
146+
++ans;
147+
}
136148
}
137149
return ans;
138150
}
@@ -142,21 +154,42 @@ public:
142154
#### Go
143155
144156
```go
145-
func meetRequirement(n int, lights [][]int, requirement []int) int {
146-
d := make([]int, 100010)
157+
func meetRequirement(n int, lights [][]int, requirement []int) (ans int) {
158+
d := make([]int, n+1)
147159
for _, e := range lights {
148160
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
149161
d[i]++
150162
d[j+1]--
151163
}
152-
var s, ans int
164+
s := 0
153165
for i, r := range requirement {
154166
s += d[i]
155167
if s >= r {
156168
ans++
157169
}
158170
}
159-
return ans
171+
return
172+
}
173+
```
174+
175+
#### TypeScript
176+
177+
```ts
178+
function meetRequirement(n: number, lights: number[][], requirement: number[]): number {
179+
const d: number[] = Array(n + 1).fill(0);
180+
for (const [p, r] of lights) {
181+
const [i, j] = [Math.max(0, p - r), Math.min(n - 1, p + r)];
182+
++d[i];
183+
--d[j + 1];
184+
}
185+
let [ans, s] = [0, 0];
186+
for (let i = 0; i < n; ++i) {
187+
s += d[i];
188+
if (s >= requirement[i]) {
189+
++ans;
190+
}
191+
}
192+
return ans;
160193
}
161194
```
162195

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
class Solution {
22
public:
33
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
4-
vector<int> d(100010);
5-
for (auto& e : lights) {
4+
vector<int> d(n + 1);
5+
for (constauto& e : lights) {
66
int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
77
++d[i];
88
--d[j + 1];
99
}
1010
int s = 0, ans = 0;
1111
for (int i = 0; i < n; ++i) {
1212
s += d[i];
13-
if (s >= requirement[i]) ++ans;
13+
if (s >= requirement[i]) {
14+
++ans;
15+
}
1416
}
1517
return ans;
1618
}
17-
};
19+
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
func meetRequirement(n int, lights [][]int, requirement []int) int {
2-
d := make([]int, 100010)
1+
func meetRequirement(n int, lights [][]int, requirement []int) (ansint) {
2+
d := make([]int, n+1)
33
for _, e := range lights {
44
i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
55
d[i]++
66
d[j+1]--
77
}
8-
vars, ansint
8+
s:=0
99
for i, r := range requirement {
1010
s += d[i]
1111
if s >= r {
1212
ans++
1313
}
1414
}
15-
returnans
16-
}
15+
return
16+
}

‎solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public int meetRequirement(int n, int[][] lights, int[] requirement) {
3-
int[] d = new int[100010];
3+
int[] d = new int[n + 1];
44
for (int[] e : lights) {
55
int i = Math.max(0, e[0] - e[1]);
66
int j = Math.min(n - 1, e[0] + e[1]);
@@ -17,4 +17,4 @@ public int meetRequirement(int n, int[][] lights, int[] requirement) {
1717
}
1818
return ans;
1919
}
20-
}
20+
}

‎solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def meetRequirement(
33
self, n: int, lights: List[List[int]], requirement: List[int]
44
) -> int:
5-
d = [0] * 100010
5+
d = [0] * (n+1)
66
for p, r in lights:
77
i, j = max(0, p - r), min(n - 1, p + r)
88
d[i] += 1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function meetRequirement(n: number, lights: number[][], requirement: number[]): number {
2+
const d: number[] = Array(n + 1).fill(0);
3+
for (const [p, r] of lights) {
4+
const [i, j] = [Math.max(0, p - r), Math.min(n - 1, p + r)];
5+
++d[i];
6+
--d[j + 1];
7+
}
8+
let [ans, s] = [0, 0];
9+
for (let i = 0; i < n; ++i) {
10+
s += d[i];
11+
if (s >= requirement[i]) {
12+
++ans;
13+
}
14+
}
15+
return ans;
16+
}

‎solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ Thus, the closest number to 0 in the array is 1.
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: One Pass
60+
61+
We define a variable $d$ to record the current minimum distance, initially $d=\infty$. Then we traverse the array, for each element $x,ドル we calculate $y=|x|$. If $y \lt d$ or $y=d$ and $x \gt ans,ドル we update the answer $ans=x$ and $d=y$.
62+
63+
After the traversal, return the answer.
64+
65+
Time complexity is $O(n),ドル where $n$ is the length of the array. Space complexity is $O(1)$.
6066

6167
<!-- tabs:start -->
6268

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /