Skip to content

feat: add solutions to lc problems: No.3091,3111 #3338

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

Merged
merged 1 commit into from
Jul 30, 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 @@ -163,6 +163,22 @@ function minOperations(k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(k: i32) -> i32 {
let mut ans = k;
for a in 0..k {
let x = a + 1;
let b = (k + x - 1) / x - 1;
ans = ans.min(a + b);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ function minOperations(k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(k: i32) -> i32 {
let mut ans = k;
for a in 0..k {
let x = a + 1;
let b = (k + x - 1) / x - 1;
ans = ans.min(a + b);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn min_operations(k: i32) -> i32 {
let mut ans = k;
for a in 0..k {
let x = a + 1;
let b = (k + x - 1) / x - 1;
ans = ans.min(a + b);
}
ans
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ tags:

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

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

遍历完成后,我们就得到了最少需要多少个矩形。
接下来我们遍历所有的点,如果当前点的横坐标 $x$ 大于 $x_1$,说明已有的矩形无法覆盖当前点,我们就需要增加一个矩形,答案加一,然后我们更新 $x_1 = x + w$。

遍历完成后,我们就得到了最少需要的矩形数目。

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

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

Expand All @@ -141,13 +143,12 @@ class Solution:
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);
int ans = 0, x1 = -1;
for (int[] p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -162,12 +163,12 @@ 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 ans = 0, x1 = -1;
for (const auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -180,11 +181,11 @@ public:
```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)
x1 := -1
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
if x := p[0]; x > x1 {
ans++
x1 = x + w
}
}
return
Expand All @@ -196,12 +197,11 @@ func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
```ts
function minRectanglesToCoverPoints(points: number[][], w: number): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
let x1 = -Infinity;
let [ans, x1] = [0, -1];
for (const [x, _] of points) {
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -215,19 +215,38 @@ impl Solution {
pub fn min_rectangles_to_cover_points(mut points: Vec<Vec<i32>>, w: i32) -> i32 {
points.sort_by(|a, b| a[0].cmp(&b[0]));
let mut ans = 0;
let mut x1 = -(1 << 30);
let mut x1 = -1;
for p in points {
let x = p[0];
if x1 + w < x {
x1 = x;
if x > x1 {
ans += 1;
x1 = x + w;
}
}
ans
}
}
```

#### C#

```cs
public class Solution {
public int MinRectanglesToCoverPoints(int[][] points, int w) {
Array.Sort(points, (a, b) => a[0] - b[0]);
int ans = 0, x1 = -1;
foreach (int[] p in points) {
int x = p[0];
if (x > x1) {
ans++;
x1 = x + w;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ tags:

### Solution 1: Greedy + Sorting

According to the problem description, we don't need to consider the height of the rectangle, only the width.
According to the problem description, we do not need to consider the height of the rectangles, 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.
We can sort all the points by their x-coordinates and use a variable $x_1$ to record the rightmost x-coordinate that the current rectangle can cover. Initially, $x_1 = -1$.

After the traversal, we get the minimum number of rectangles needed.
Next, we iterate through all the points. If the current point's x-coordinate $x$ is greater than $x_1$, it means the existing rectangle cannot cover the current point. We need to add a new rectangle, increment the answer by one, and update $x_1 = x + w$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the number of points.
After completing the iteration, we obtain the minimum number of rectangles needed.

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

<!-- tabs:start -->

Expand All @@ -170,11 +172,11 @@ The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log
class Solution:
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
points.sort()
ans, x1 = 0, -inf
ans, x1 = 0, -1
for x, _ in points:
if x1 + w < x:
x1 = x
if x > x1:
ans += 1
x1 = x + w
return ans
```

Expand All @@ -184,13 +186,12 @@ class Solution:
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);
int ans = 0, x1 = -1;
for (int[] p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -205,12 +206,12 @@ 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 ans = 0, x1 = -1;
for (const auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -223,11 +224,11 @@ public:
```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)
x1 := -1
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
if x := p[0]; x > x1 {
ans++
x1 = x + w
}
}
return
Expand All @@ -239,12 +240,11 @@ func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
```ts
function minRectanglesToCoverPoints(points: number[][], w: number): number {
points.sort((a, b) => a[0] - b[0]);
let ans = 0;
let x1 = -Infinity;
let [ans, x1] = [0, -1];
for (const [x, _] of points) {
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
Expand All @@ -258,19 +258,38 @@ impl Solution {
pub fn min_rectangles_to_cover_points(mut points: Vec<Vec<i32>>, w: i32) -> i32 {
points.sort_by(|a, b| a[0].cmp(&b[0]));
let mut ans = 0;
let mut x1 = -(1 << 30);
let mut x1 = -1;
for p in points {
let x = p[0];
if x1 + w < x {
x1 = x;
if x > x1 {
ans += 1;
x1 = x + w;
}
}
ans
}
}
```

#### C#

```cs
public class Solution {
public int MinRectanglesToCoverPoints(int[][] points, int w) {
Array.Sort(points, (a, b) => a[0] - b[0]);
int ans = 0, x1 = -1;
foreach (int[] p in points) {
int x = p[0];
if (x > x1) {
ans++;
x1 = x + w;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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 ans = 0, x1 = -1;
for (const auto& p : points) {
int x = p[0];
if (x1 + w < x) {
x1 = x;
if (x > x1) {
++ans;
x1 = x + w;
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public int MinRectanglesToCoverPoints(int[][] points, int w) {
Array.Sort(points, (a, b) => a[0] - b[0]);
int ans = 0, x1 = -1;
foreach (int[] p in points) {
int x = p[0];
if (x > x1) {
ans++;
x1 = x + w;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +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)
x1 := -1
for _, p := range points {
if x := p[0]; x1+w < x {
x1 = x
if x := p[0]; x > x1 {
ans++
x1 = x + w
}
}
return
}
}
Loading
Loading