# 587. Erect the Fence (Hard)

https://leetcode.com/problems/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.

## Solutions

``````/**
* Definition for a point.
* class Point {
*     int x;
*     int y;
*     Point() { x = 0; y = 0; }
*     Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
private class PointsComparator implements Comparator<Point> {
private Point org;
public PointsComparator(int x, int y) {
org = new Point(x, y);
}

public int compare(Point a, Point b) {
int c = crossProduct(org, a, b);
if(c > 0) return -1;
else if(c < 0) return 1;
else {
int disa = (org.x - a.x) * (org.x - a.x) + (org.y - a.y) * (org.y - a.y);
int disb = (org.x - b.x) * (org.x - b.x) + (org.y - b.y) * (org.y - b.y);
return disa - disb;
}
}
}

private int crossProduct(Point org, Point a, Point b) {
return (a.x - org.x) * (b.y - org.y) - (b.x - org.x) * (a.y - org.y);
}

public List<Point> outerTrees(Point[] points) {
int n = points.length;
List<Point> ret = new ArrayList<>();
if(n <= 3) {
for(int i = 0;i < n;++i) {
}
return ret;
}
int minXY = 0;
for(int i = 1;i < n;++i) {
if(points[i].y < points[minXY].y) {
minXY = i;
} else if(points[i].y == points[minXY].y && points[i].x < points[minXY].x) {
minXY = i;
}
}

if(minXY != 0) {
Point t = points;
points = points[minXY];
points[minXY] = t;
}

Arrays.sort(points, 1, n, new PointsComparator(points.x, points.y));

////////
// this part is to avoid complexity for the points with the largest angel to points
//https://leetcode.com/problems/erect-the-fence/#/solutions
int idx = n - 2;
while(idx >= 0 && crossProduct(points, points[n - 1], points[idx]) == 0) {
idx--;
}
int i = idx + 1;
int j = n - 1;
while(i < j) {
Point t = points[i];
points[i] = points[j];
points[j] = t;
i++;
j--;
}
//////////

i = 2;
while(i < n) {
while(ret.size() >= 2) {
int c = crossProduct(ret.get(ret.size() - 2), ret.get(ret.size() - 1), points[i]);
if(c >= 0) {
break;
} else {
ret.remove(ret.size() - 1);
}
}