[Leetcode] 587. Erect the Fence 解题报告

题目

There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

Example 1:

Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
Explanation:

Example 2:

Input: [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
Explanation:

Even you only have trees in a line, you need to use rope to enclose them. 

Note:

  1. All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
  2. All input integers will range from 0 to 100.
  3. The garden has at least one tree.
  4. All coordinates are distinct.
  5. Input points have NO order. No order required for output.

思路

这是一道求凸包的题目,是计算几何中的经典问题。凸包问题有好几种解法,下面的代码只是给出了其中一种。更详细的说明,读者可以参照:凸包问题的五种解法

代码

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */
class Solution {
public:
    vector<Point> outerTrees(vector<Point>& points) {
        if(points.size() < 3) {
            return points;
        }
        auto cmp = [](Point& a, Point& b) -> bool {
            return a.x < b.x || (a.x == b.x && a.y < b.y);
        };
        sort(points.begin(), points.end(), cmp);
        vector<Point> stack;
        stack.push_back(points[0]), stack.push_back(points[1]);
        for(int i = 2; i < points.size(); ++i) {    //left to right;
            while(stack.size() > 1) {
                auto &t1 = stack.back();
                auto &t2 = stack[stack.size() - 2];
                if(isRightTurn(t2, t1, points[i])) {
                    break;
                }
                else {
                    stack.pop_back();
                }
            }
            stack.push_back(points[i]);
        }
        int n = stack.size();
        if(n == points.size()) {
            return stack; //check if linear
        }
        stack.push_back(points[points.size() - 2]);
        for(int i = points.size() - 3; i >= 0; --i) {   //right to left;
            while(stack.size() > n) {
                auto &t1 = stack.back();
                auto &t2 = stack[stack.size() - 2];
                if(isRightTurn(t2, t1, points[i])) {
                    break;
                }
                else {
                    stack.pop_back();
                }
            }
            stack.push_back(points[i]);
        }
        stack.pop_back();
        return stack;
    }
private:
    bool isRightTurn(Point &a, Point &b, Point &c) {
        return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) <= 0;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值