forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erect-the-fence.cpp
56 lines (50 loc) · 1.77 KB
/
erect-the-fence.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
// Time: O(nlogn)
// Space: O(n)
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
// Monotone Chain Algorithm
class Solution {
public:
vector<Point> outerTrees(vector<Point>& points) {
const auto orientation = [](const Point& p, const Point& q, const Point& r) {
return (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
};
const auto cmp = [](const Point& p, const Point& q) {
return p.x == q.x ? p.y < q.y : p.x < q.x;
};
const auto eq = [](const Point &p1, const Point &p2) {
return p1.x == p2.x && p1.y == p2.y;
};
vector<Point> hull;
sort(points.begin(), points.end(), cmp);
for (int i = 0; i < points.size(); ++i) {
while (hull.size() >= 2 &&
orientation(hull[hull.size() - 2],
hull[hull.size() - 1],
points[i]) > 0) {
hull.pop_back();
}
hull.emplace_back(points[i]);
}
for (int i = points.size() - 1; i >= 0; --i) {
while (hull.size() >= 2 &&
orientation(hull[hull.size() - 2],
hull[hull.size() - 1],
points[i]) > 0) {
hull.pop_back();
}
hull.emplace_back(points[i]);
}
sort(hull.begin(), hull.end(), cmp);
hull.erase(unique(hull.begin(), hull.end(), eq), hull.end());
return hull;
}
};