代码拉取完成,页面将自动刷新
// 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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。