|
134 | 134 |
|
135 | 135 | ## Solutions
|
136 | 136 |
|
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. |
138 | 146 |
|
139 | 147 | <!-- tabs:start -->
|
140 | 148 |
|
141 | 149 | ```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 |
143 | 159 | ```
|
144 | 160 |
|
145 | 161 | ```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 | +} |
147 | 177 | ```
|
148 | 178 |
|
149 | 179 | ```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 | +}; |
151 | 195 | ```
|
152 | 196 |
|
153 | 197 | ```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 | +``` |
154 | 210 |
|
| 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 | +} |
155 | 224 | ```
|
156 | 225 |
|
157 | 226 | <!-- tabs:end -->
|
|
0 commit comments