Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.3111,3112 #2583

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,93 @@

## 解法

### 方法一
### 方法一:贪心 + 排序

根据题目描述,我们不需要考虑矩形的高度,只需要考虑矩形的宽度。

我们可以将所有的点按照横坐标进行排序,用一个变量 $x_1$ 记录当前矩形的左下角的横坐标。然后遍历所有的点,如果当前点的横坐标 $x$ 比 $x_1 + w$ 大,说明当前点不能被当前的矩形覆盖,我们就需要增加一个新的矩形,然后更新 $x_1$ 为当前点的横坐标。

遍历完成后,我们就得到了最少需要多少个矩形。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是点的数量。

<!-- tabs:start -->

```python

class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points.sort()
ans, x1 = 0, -inf
for x, _ in points:
if x1 + w < x:
x1 = x
ans += 1
return ans
```

```java

class Solution {
public int minRectanglesToCoverPoints(int[][] points, int w) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int ans = 0;
int x1 = -(1 << 30);
for (int[] p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
sort(points.begin(), points.end());
int ans = 0, x1 = -(1 << 30);
for (auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
};
```

```go
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
x1 := -(1 << 30)
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
ans++
}
}
return
}
```

```ts
function minRectanglesToCoverPoints(points: number[][], w: number): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
let x1 = -Infinity;
for (const [x, _] of points) {
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,93 @@

## Solutions

### Solution 1
### Solution 1: Greedy + Sorting

According to the problem description, we don't need to consider the height of the rectangle, only the width.

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.

After the traversal, we get the minimum number of rectangles needed.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of points.

<!-- tabs:start -->

```python

class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points.sort()
ans, x1 = 0, -inf
for x, _ in points:
if x1 + w < x:
x1 = x
ans += 1
return ans
```

```java

class Solution {
public int minRectanglesToCoverPoints(int[][] points, int w) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int ans = 0;
int x1 = -(1 << 30);
for (int[] p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
sort(points.begin(), points.end());
int ans = 0, x1 = -(1 << 30);
for (auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
};
```

```go
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
x1 := -(1 << 30)
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
ans++
}
}
return
}
```

```ts
function minRectanglesToCoverPoints(points: number[][], w: number): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
let x1 = -Infinity;
for (const [x, _] of points) {
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
sort(points.begin(), points.end());
int ans = 0, x1 = -(1 << 30);
for (auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
x1 := -(1 << 30)
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
ans++
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int minRectanglesToCoverPoints(int[][] points, int w) {
Arrays.sort(points, (a, b) -> a[0] - b[0]);
int ans = 0;
int x1 = -(1 << 30);
for (int[] p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points.sort()
ans, x1 = 0, -inf
for x, _ in points:
if x1 + w < x:
x1 = x
ans += 1
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function minRectanglesToCoverPoints(points: number[][], w: number): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
let x1 = -Infinity;
for (const [x, _] of points) {
if (x1 + w < x) {
x1 = x;
++ans;
}
}
return ans;
}
Loading
Loading