Yola
BAN USER
For one who would like to use C++.
bool idistant_internal(std::vector<int>& arr, int curr) {
if (curr == 0) return true;
for (int pos = 0; pos < (int)arr.size() - curr - 1; ++pos) {
if (arr[pos] == 0 && arr[pos + curr + 1] == 0) {
arr[pos] = arr[pos + curr + 1] = curr;
if (idistant_internal(arr, curr - 1)) return true;
arr[pos] = arr[pos + curr + 1] = 0;
}
}
return false;
}
std::vector<int> idistant(int n) {
std::vector<int> res(2*n);
if (idistant_internal(res, n)) return res;
return {};
}
You can do it in O(n*lg(n)) with a sweep line algorithm.
> Sort all rectangles left x and all points by x.
> Move sweep line with 3 kinds of event points: start/end of rectangle and point.
> If left/right rectangle boundary is encountered then add/remove its y-interval to a line. Can be done in log(n) because rectangles don't intersect.
> If a point is encountered then check to which y-interval it belongs. Can be done in log(n) because intervals on the line stored sorted.
It is m^n and m*(m+1)^(n-1).
- Yola October 08, 2022