-
Notifications
You must be signed in to change notification settings - Fork 0
/
Erect the Fence.cpp
56 lines (42 loc) · 1.4 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
class Solution {
public:
int findValid(pair<int, int> &P1, pair<int, int> &P2, pair<int, int> P3){
int x1 = P1.first;
int y1 = P1.second;
int x2 = P2.first;
int y2 = P2.second;
int x3 = P3.first;
int y3 = P3.second;
return ((y3-y2)*(x2-x1) - (y2-y1)*(x3-x2));
}
vector<vector<int>> outerTrees(vector<vector<int>>& trees) {
vector<vector<int>> result;
sort(trees.begin(), trees.end());
deque<pair<int , int>> lower, upper;
for(auto &point : trees){
int l = lower.size();
int u = upper.size();
while( l >= 2 && findValid(lower[l-2], lower[l-1], {point[0], point[1]}) < 0){
l--;
lower.pop_back();
}
while( u >= 2 && findValid(upper[u-2], upper[u-1], {point[0], point[1]}) > 0){
u--;
upper.pop_back();
}
upper.push_back({point[0], point[1]});
lower.push_back({point[0], point[1]});
}
set<pair<int, int>> st;
for(auto &pair : upper){
st.insert(pair);
}
for(auto &pair : lower){
st.insert(pair);
}
for(auto &pair : st){
result.push_back({pair.first, pair.second});
}
return result;
}
};