diff --git a/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README.md b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README.md index 480e2c2e9090e..a2bbdb2203b46 100644 --- a/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README.md +++ b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README.md @@ -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 + } +} +``` + diff --git a/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README_EN.md b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README_EN.md index e770afb61bf0d..aef9ff3dde83c 100644 --- a/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README_EN.md +++ b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/README_EN.md @@ -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 + } +} +``` + diff --git a/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/Solution.rs b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/Solution.rs new file mode 100644 index 0000000000000..869990a5f085f --- /dev/null +++ b/solution/3000-3099/3091.Apply Operations to Make Sum of Array Greater Than or Equal to k/Solution.rs @@ -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 + } +} diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md index 373a52f7c8ac4..0204d8fcb3954 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README.md @@ -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$ 是点的数量。 @@ -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 ``` @@ -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; @@ -162,12 +163,12 @@ class Solution { public: int minRectanglesToCoverPoints(vector>& 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; @@ -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 @@ -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; @@ -215,12 +215,12 @@ impl Solution { pub fn min_rectangles_to_cover_points(mut points: Vec>, 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 @@ -228,6 +228,25 @@ impl Solution { } ``` +#### 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; + } +} +``` + diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md index a595aa926003d..e241e0556a299 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/README_EN.md @@ -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. @@ -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 ``` @@ -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; @@ -205,12 +206,12 @@ class Solution { public: int minRectanglesToCoverPoints(vector>& 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; @@ -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 @@ -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; @@ -258,12 +258,12 @@ impl Solution { pub fn min_rectangles_to_cover_points(mut points: Vec>, 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 @@ -271,6 +271,25 @@ impl Solution { } ``` +#### 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; + } +} +``` + diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp index cb90fdb4e6c24..d20c6cb6b24f8 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cpp @@ -2,14 +2,14 @@ class Solution { public: int minRectanglesToCoverPoints(vector>& 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; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cs b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cs new file mode 100644 index 0000000000000..6d8d1b4aaab5d --- /dev/null +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.cs @@ -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; + } +} diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go index 576eb533bc39b..7296c2289104d 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.go @@ -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 -} \ No newline at end of file +} diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java index 8f965d1e11c45..453d47b2bcf5f 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.java @@ -1,15 +1,14 @@ 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; } -} \ No newline at end of file +} diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py index 8f7b702ffadfd..6589281e14900 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.py @@ -1,9 +1,9 @@ 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 diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.rs b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.rs index 898d024b0830f..1a3220313c400 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.rs +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.rs @@ -2,12 +2,12 @@ impl Solution { pub fn min_rectangles_to_cover_points(mut points: Vec>, 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 diff --git a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts index e59de2938075f..93859174a06c3 100644 --- a/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts +++ b/solution/3100-3199/3111.Minimum Rectangles to Cover Points/Solution.ts @@ -1,11 +1,10 @@ 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;