3111. Minimum Rectangles to Cover Points
class Solution {
public:
int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
vector<int> vec_x;
for (auto const& v : points) vec_x.push_back(v[0]);
sort(vec_x.begin(), vec_x.end());
if (vec_x.size() == 1) return 1;
if (vec_x[vec_x.size() - 1] - vec_x[0] <= w) return 1;
// Count the min of rectangles
const int last = vec_x[vec_x.size() - 1];
int rec_count = 0, start_x = vec_x[0];
for (int i = 0; i < vec_x.size(); ++i) {
start_x = vec_x[i];
const int next_x = start_x + w;
if (next_x >= last) {
++rec_count;
break;
}
while (i < vec_x.size() && vec_x[i] <= next_x) i += 1;
--i;
++rec_count;
}
return rec_count;
}
};
Last updated