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 d1c9ae8

Browse files
feat: add solutions to lc problems: No.3111,3112 (doocs#2583)
* No.3111.Minimum Rectangles to Cover Points * No.3112.Minimum Time to Visit Disappearing Nodes
1 parent abc12e7 commit d1c9ae8

File tree

13 files changed

+683
-18
lines changed

13 files changed

+683
-18
lines changed

‎solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md‎

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,93 @@
9393

9494
## 解法
9595

96-
### 方法一
96+
### 方法一:贪心 + 排序
97+
98+
根据题目描述,我们不需要考虑矩形的高度,只需要考虑矩形的宽度。
99+
100+
我们可以将所有的点按照横坐标进行排序,用一个变量 $x_1$ 记录当前矩形的左下角的横坐标。然后遍历所有的点,如果当前点的横坐标 $x$ 比 $x_1 + w$ 大,说明当前点不能被当前的矩形覆盖,我们就需要增加一个新的矩形,然后更新 $x_1$ 为当前点的横坐标。
101+
102+
遍历完成后,我们就得到了最少需要多少个矩形。
103+
104+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。
97105

98106
<!-- tabs:start -->
99107

100108
```python
101-
109+
class Solution:
110+
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
111+
points.sort()
112+
ans, x1 = 0, -inf
113+
for x, _ in points:
114+
if x1 + w < x:
115+
x1 = x
116+
ans += 1
117+
return ans
102118
```
103119

104120
```java
105-
121+
class Solution {
122+
public int minRectanglesToCoverPoints(int[][] points, int w) {
123+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
124+
int ans = 0;
125+
int x1 = -(1 << 30);
126+
for (int[] p : points) {
127+
int x = p[0];
128+
if (x1 + w < x) {
129+
x1 = x;
130+
++ans;
131+
}
132+
}
133+
return ans;
134+
}
135+
}
106136
```
107137

108138
```cpp
109-
139+
class Solution {
140+
public:
141+
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
142+
sort(points.begin(), points.end());
143+
int ans = 0, x1 = -(1 << 30);
144+
for (auto& p : points) {
145+
int x = p[0];
146+
if (x1 + w < x) {
147+
x1 = x;
148+
++ans;
149+
}
150+
}
151+
return ans;
152+
}
153+
};
110154
```
111155
112156
```go
157+
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
158+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
159+
x1 := -(1 << 30)
160+
for _, p := range points {
161+
if x := p[0]; x1+w < x {
162+
x1 = x
163+
ans++
164+
}
165+
}
166+
return
167+
}
168+
```
113169

170+
```ts
171+
function minRectanglesToCoverPoints(points: number[][], w: number): number {
172+
points.sort((a, b) => a[0] - b[0]);
173+
let ans = 0;
174+
let x1 = -Infinity;
175+
for (const [x, _] of points) {
176+
if (x1 + w < x) {
177+
x1 = x;
178+
++ans;
179+
}
180+
}
181+
return ans;
182+
}
114183
```
115184

116185
<!-- tabs:end -->

‎solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md‎

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,93 @@
134134

135135
## Solutions
136136

137-
### Solution 1
137+
### Solution 1: Greedy + Sorting
138+
139+
According to the problem description, we don't need to consider the height of the rectangle, only the width.
140+
141+
We can sort all the points according to the x-coordinate and use a variable $x_1$ to record the current x-coordinate of the lower left corner of the rectangle. Then we traverse all the points. If the x-coordinate $x$ of the current point is greater than $x_1 + w,ドル it means that the current point cannot be covered by the current rectangle. We need to add a new rectangle and update $x_1$ to the x-coordinate of the current point.
142+
143+
After the traversal, we get the minimum number of rectangles needed.
144+
145+
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n),ドル where $n$ is the number of points.
138146

139147
<!-- tabs:start -->
140148

141149
```python
142-
150+
class Solution:
151+
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
152+
points.sort()
153+
ans, x1 = 0, -inf
154+
for x, _ in points:
155+
if x1 + w < x:
156+
x1 = x
157+
ans += 1
158+
return ans
143159
```
144160

145161
```java
146-
162+
class Solution {
163+
public int minRectanglesToCoverPoints(int[][] points, int w) {
164+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
165+
int ans = 0;
166+
int x1 = -(1 << 30);
167+
for (int[] p : points) {
168+
int x = p[0];
169+
if (x1 + w < x) {
170+
x1 = x;
171+
++ans;
172+
}
173+
}
174+
return ans;
175+
}
176+
}
147177
```
148178

149179
```cpp
150-
180+
class Solution {
181+
public:
182+
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
183+
sort(points.begin(), points.end());
184+
int ans = 0, x1 = -(1 << 30);
185+
for (auto& p : points) {
186+
int x = p[0];
187+
if (x1 + w < x) {
188+
x1 = x;
189+
++ans;
190+
}
191+
}
192+
return ans;
193+
}
194+
};
151195
```
152196
153197
```go
198+
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
199+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
200+
x1 := -(1 << 30)
201+
for _, p := range points {
202+
if x := p[0]; x1+w < x {
203+
x1 = x
204+
ans++
205+
}
206+
}
207+
return
208+
}
209+
```
154210

211+
```ts
212+
function minRectanglesToCoverPoints(points: number[][], w: number): number {
213+
points.sort((a, b) => a[0] - b[0]);
214+
let ans = 0;
215+
let x1 = -Infinity;
216+
for (const [x, _] of points) {
217+
if (x1 + w < x) {
218+
x1 = x;
219+
++ans;
220+
}
221+
}
222+
return ans;
223+
}
155224
```
156225

157226
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
4+
sort(points.begin(), points.end());
5+
int ans = 0, x1 = -(1 << 30);
6+
for (auto& p : points) {
7+
int x = p[0];
8+
if (x1 + w < x) {
9+
x1 = x;
10+
++ans;
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
2+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
3+
x1 := -(1 << 30)
4+
for _, p := range points {
5+
if x := p[0]; x1+w < x {
6+
x1 = x
7+
ans++
8+
}
9+
}
10+
return
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minRectanglesToCoverPoints(int[][] points, int w) {
3+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
4+
int ans = 0;
5+
int x1 = -(1 << 30);
6+
for (int[] p : points) {
7+
int x = p[0];
8+
if (x1 + w < x) {
9+
x1 = x;
10+
++ans;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
3+
points.sort()
4+
ans, x1 = 0, -inf
5+
for x, _ in points:
6+
if x1 + w < x:
7+
x1 = x
8+
ans += 1
9+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minRectanglesToCoverPoints(points: number[][], w: number): number {
2+
points.sort((a, b) => a[0] - b[0]);
3+
let ans = 0;
4+
let x1 = -Infinity;
5+
for (const [x, _] of points) {
6+
if (x1 + w < x) {
7+
x1 = x;
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

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