forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherect-the-fence-ii.cpp
81 lines (70 loc) · 2.69 KB
/
erect-the-fence-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Time: O(n) on average
// Space: O(n)
// reference: https://en.wikipedia.org/wiki/Smallest-circle_problem
class Solution {
public:
vector<double> outerTrees(vector<vector<int>>& trees) {
random_shuffle(begin(trees), end(trees));
vector<vector<double>> boundaries;
const auto& result = Welzl(trees, &boundaries, 0);
return {result.first[0], result.first[1], result.second};
}
private:
pair<vector<double>, double> Welzl(
const vector<vector<int>>& points,
vector<vector<double>> *boundaries,
int curr) {
if (curr == size(points) || size(*boundaries) == 3) {
return trivial(*boundaries);
}
vector<double> p = {double(points[curr][0]), double(points[curr][1])};
const auto& result = Welzl(points, boundaries, curr + 1);
if (!empty(result.first) && inside(result, p)) {
return result;
}
boundaries->emplace_back(move(p));
const auto& result2 = Welzl(points, boundaries, curr + 1);
boundaries->pop_back();
return result2;
}
pair<vector<double>, double> trivial(const vector<vector<double>>& boundaries) { // circumscribed circle
if (empty(boundaries)) {
return {{}, 0.0};
}
if (size(boundaries) == 1) {
return {boundaries[0], 0.0};
}
if (size(boundaries) == 2) {
return circle_from_2_points(boundaries[0], boundaries[1]);
}
return circle_from_3_points(boundaries[0], boundaries[1], boundaries[2]);
}
double dist(const vector<double>& a, const vector<double>& b) {
return sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));
}
bool inside(const pair<vector<double>, double>& c, const vector<double>& p) {
static const double EPS = 1e-5;
return dist(c.first, p) < c.second + EPS;
}
vector<double> circle_center(int bx, int by, int cx, int cy) {
double B = bx * bx + by * by;
double C = cx * cx + cy * cy;
double D = bx * cy - by * cx;
return {(cy * B - by * C) / (2 * D), (bx * C - cx * B) / (2 * D)};
}
pair<vector<double>, double> circle_from_2_points(
const vector<double>& A,
const vector<double>& B) {
vector<double> C = {(A[0] + B[0]) / 2.0, (A[1] + B[1]) / 2.0};
return {C, dist(A, B) / 2.0};
}
pair<vector<double>, double> circle_from_3_points(
const vector<double>& A,
const vector<double>& B,
const vector<double>& C) {
vector<double> I = circle_center(B[0] - A[0], B[1] - A[1], C[0] - A[0], C[1] - A[1]);
I[0] += A[0];
I[1] += A[1];
return {I, dist(I, A)};
}
};