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 32ac4a4

Browse files
feat: add solutions to lc problems: No.3091,3111 (doocs#3338)
* No.3091.Apply Operations to Make Sum of Array Greater Than or Equal to k * No.3111.Minimum Rectangles to Cover Points
1 parent 4012ecc commit 32ac4a4

File tree

12 files changed

+165
-72
lines changed

12 files changed

+165
-72
lines changed

‎solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ function minOperations(k: number): number {
163163
}
164164
```
165165

166+
#### Rust
167+
168+
```rust
169+
impl Solution {
170+
pub fn min_operations(k: i32) -> i32 {
171+
let mut ans = k;
172+
for a in 0..k {
173+
let x = a + 1;
174+
let b = (k + x - 1) / x - 1;
175+
ans = ans.min(a + b);
176+
}
177+
ans
178+
}
179+
}
180+
```
181+
166182
<!-- tabs:end -->
167183

168184
<!-- solution:end -->

‎solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README_EN.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ function minOperations(k: number): number {
161161
}
162162
```
163163

164+
#### Rust
165+
166+
```rust
167+
impl Solution {
168+
pub fn min_operations(k: i32) -> i32 {
169+
let mut ans = k;
170+
for a in 0..k {
171+
let x = a + 1;
172+
let b = (k + x - 1) / x - 1;
173+
ans = ans.min(a + b);
174+
}
175+
ans
176+
}
177+
}
178+
```
179+
164180
<!-- tabs:end -->
165181

166182
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn min_operations(k: i32) -> i32 {
3+
let mut ans = k;
4+
for a in 0..k {
5+
let x = a + 1;
6+
let b = (k + x - 1) / x - 1;
7+
ans = ans.min(a + b);
8+
}
9+
ans
10+
}
11+
}

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

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ tags:
113113

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

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

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

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

@@ -127,11 +129,11 @@ tags:
127129
class Solution:
128130
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
129131
points.sort()
130-
ans, x1 = 0, -inf
132+
ans, x1 = 0, -1
131133
for x, _ in points:
132-
if x1 + w < x:
133-
x1 = x
134+
if x > x1:
134135
ans += 1
136+
x1 = x + w
135137
return ans
136138
```
137139

@@ -141,13 +143,12 @@ class Solution:
141143
class Solution {
142144
public int minRectanglesToCoverPoints(int[][] points, int w) {
143145
Arrays.sort(points, (a, b) -> a[0] - b[0]);
144-
int ans = 0;
145-
int x1 = -(1 << 30);
146+
int ans = 0, x1 = -1;
146147
for (int[] p : points) {
147148
int x = p[0];
148-
if (x1 + w < x) {
149-
x1 = x;
149+
if (x > x1) {
150150
++ans;
151+
x1 = x + w;
151152
}
152153
}
153154
return ans;
@@ -162,12 +163,12 @@ class Solution {
162163
public:
163164
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
164165
sort(points.begin(), points.end());
165-
int ans = 0, x1 = -(1 << 30);
166-
for (auto& p : points) {
166+
int ans = 0, x1 = -1;
167+
for (const auto& p : points) {
167168
int x = p[0];
168-
if (x1 + w < x) {
169-
x1 = x;
169+
if (x > x1) {
170170
++ans;
171+
x1 = x + w;
171172
}
172173
}
173174
return ans;
@@ -180,11 +181,11 @@ public:
180181
```go
181182
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
182183
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
183-
x1 := -(1 << 30)
184+
x1 := -1
184185
for _, p := range points {
185-
if x := p[0]; x1+w < x {
186-
x1 = x
186+
if x := p[0]; x > x1 {
187187
ans++
188+
x1 = x + w
188189
}
189190
}
190191
return
@@ -196,12 +197,11 @@ func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
196197
```ts
197198
function minRectanglesToCoverPoints(points: number[][], w: number): number {
198199
points.sort((a, b) => a[0] - b[0]);
199-
let ans = 0;
200-
let x1 = -Infinity;
200+
let [ans, x1] = [0, -1];
201201
for (const [x, _] of points) {
202-
if (x1 + w < x) {
203-
x1 = x;
202+
if (x > x1) {
204203
++ans;
204+
x1 = x + w;
205205
}
206206
}
207207
return ans;
@@ -215,19 +215,38 @@ impl Solution {
215215
pub fn min_rectangles_to_cover_points(mut points: Vec<Vec<i32>>, w: i32) -> i32 {
216216
points.sort_by(|a, b| a[0].cmp(&b[0]));
217217
let mut ans = 0;
218-
let mut x1 = -(1<<30);
218+
let mut x1 = -1;
219219
for p in points {
220220
let x = p[0];
221-
if x1 + w < x {
222-
x1 = x;
221+
if x > x1 {
223222
ans += 1;
223+
x1 = x + w;
224224
}
225225
}
226226
ans
227227
}
228228
}
229229
```
230230

231+
#### C#
232+
233+
```cs
234+
public class Solution {
235+
public int MinRectanglesToCoverPoints(int[][] points, int w) {
236+
Array.Sort(points, (a, b) => a[0] - b[0]);
237+
int ans = 0, x1 = -1;
238+
foreach (int[] p in points) {
239+
int x = p[0];
240+
if (x > x1) {
241+
ans++;
242+
x1 = x + w;
243+
}
244+
}
245+
return ans;
246+
}
247+
}
248+
```
249+
231250
<!-- tabs:end -->
232251

233252
<!-- solution:end -->

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

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ tags:
154154

155155
### Solution 1: Greedy + Sorting
156156

157-
According to the problem description, we don't need to consider the height of the rectangle, only the width.
157+
According to the problem description, we do not need to consider the height of the rectangles, only the width.
158158

159-
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.
159+
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$.
160160

161-
After the traversal, we get the minimum number of rectangles needed.
161+
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$.
162162

163-
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n),ドル where $n$ is the number of points.
163+
After completing the iteration, we obtain the minimum number of rectangles needed.
164+
165+
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n)$. Here, $n$ is the number of points.
164166

165167
<!-- tabs:start -->
166168

@@ -170,11 +172,11 @@ The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log
170172
class Solution:
171173
def minRectanglesToCoverPoints(self, points: List[List[int]], w: int) -> int:
172174
points.sort()
173-
ans, x1 = 0, -inf
175+
ans, x1 = 0, -1
174176
for x, _ in points:
175-
if x1 + w < x:
176-
x1 = x
177+
if x > x1:
177178
ans += 1
179+
x1 = x + w
178180
return ans
179181
```
180182

@@ -184,13 +186,12 @@ class Solution:
184186
class Solution {
185187
public int minRectanglesToCoverPoints(int[][] points, int w) {
186188
Arrays.sort(points, (a, b) -> a[0] - b[0]);
187-
int ans = 0;
188-
int x1 = -(1 << 30);
189+
int ans = 0, x1 = -1;
189190
for (int[] p : points) {
190191
int x = p[0];
191-
if (x1 + w < x) {
192-
x1 = x;
192+
if (x > x1) {
193193
++ans;
194+
x1 = x + w;
194195
}
195196
}
196197
return ans;
@@ -205,12 +206,12 @@ class Solution {
205206
public:
206207
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
207208
sort(points.begin(), points.end());
208-
int ans = 0, x1 = -(1 << 30);
209-
for (auto& p : points) {
209+
int ans = 0, x1 = -1;
210+
for (const auto& p : points) {
210211
int x = p[0];
211-
if (x1 + w < x) {
212-
x1 = x;
212+
if (x > x1) {
213213
++ans;
214+
x1 = x + w;
214215
}
215216
}
216217
return ans;
@@ -223,11 +224,11 @@ public:
223224
```go
224225
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
225226
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
226-
x1 := -(1 << 30)
227+
x1 := -1
227228
for _, p := range points {
228-
if x := p[0]; x1+w < x {
229-
x1 = x
229+
if x := p[0]; x > x1 {
230230
ans++
231+
x1 = x + w
231232
}
232233
}
233234
return
@@ -239,12 +240,11 @@ func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
239240
```ts
240241
function minRectanglesToCoverPoints(points: number[][], w: number): number {
241242
points.sort((a, b) => a[0] - b[0]);
242-
let ans = 0;
243-
let x1 = -Infinity;
243+
let [ans, x1] = [0, -1];
244244
for (const [x, _] of points) {
245-
if (x1 + w < x) {
246-
x1 = x;
245+
if (x > x1) {
247246
++ans;
247+
x1 = x + w;
248248
}
249249
}
250250
return ans;
@@ -258,19 +258,38 @@ impl Solution {
258258
pub fn min_rectangles_to_cover_points(mut points: Vec<Vec<i32>>, w: i32) -> i32 {
259259
points.sort_by(|a, b| a[0].cmp(&b[0]));
260260
let mut ans = 0;
261-
let mut x1 = -(1<<30);
261+
let mut x1 = -1;
262262
for p in points {
263263
let x = p[0];
264-
if x1 + w < x {
265-
x1 = x;
264+
if x > x1 {
266265
ans += 1;
266+
x1 = x + w;
267267
}
268268
}
269269
ans
270270
}
271271
}
272272
```
273273

274+
#### C#
275+
276+
```cs
277+
public class Solution {
278+
public int MinRectanglesToCoverPoints(int[][] points, int w) {
279+
Array.Sort(points, (a, b) => a[0] - b[0]);
280+
int ans = 0, x1 = -1;
281+
foreach (int[] p in points) {
282+
int x = p[0];
283+
if (x > x1) {
284+
ans++;
285+
x1 = x + w;
286+
}
287+
}
288+
return ans;
289+
}
290+
}
291+
```
292+
274293
<!-- tabs:end -->
275294

276295
<!-- solution:end -->

‎solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ class Solution {
22
public:
33
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
44
sort(points.begin(), points.end());
5-
int ans = 0, x1 = -(1 << 30);
6-
for (auto& p : points) {
5+
int ans = 0, x1 = -1;
6+
for (constauto& p : points) {
77
int x = p[0];
8-
if (x1 + w < x) {
9-
x1 = x;
8+
if (x > x1) {
109
++ans;
10+
x1 = x + w;
1111
}
1212
}
1313
return ans;
1414
}
15-
};
15+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int MinRectanglesToCoverPoints(int[][] points, int w) {
3+
Array.Sort(points, (a, b) => a[0] - b[0]);
4+
int ans = 0, x1 = -1;
5+
foreach (int[] p in points) {
6+
int x = p[0];
7+
if (x > x1) {
8+
ans++;
9+
x1 = x + w;
10+
}
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func minRectanglesToCoverPoints(points [][]int, w int) (ans int) {
22
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
3-
x1 := -(1<<30)
3+
x1 := -1
44
for _, p := range points {
5-
if x := p[0]; x1+w < x {
6-
x1 = x
5+
if x := p[0]; x > x1 {
76
ans++
7+
x1 = x + w
88
}
99
}
1010
return
11-
}
11+
}

0 commit comments

Comments
(0)

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