Skip to content

Commit 5e32a35

Browse files
authored
Create minimum-rectangles-to-cover-points.cpp
1 parent 5efbce1 commit 5e32a35

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
// sort, greedy
5+
class Solution {
6+
public:
7+
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
8+
sort(begin(points), end(points), [](const auto& a, const auto& b) {
9+
return a[0] < b[0];
10+
});
11+
int result = 0;
12+
int left = -(w + 1);
13+
for (const auto& p : points) {
14+
if (p[0] - left <= w) {
15+
continue;
16+
}
17+
left = p[0];
18+
++result;
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)