forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
67 lines (51 loc) · 1.83 KB
/
main2.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
57
58
59
60
61
62
63
64
65
66
67
/// Source : https://leetcode.com/problems/erect-the-fence/
/// Author : liuyubobobo
/// Time : 2020-05-01
#include <iostream>
#include <vector>
using namespace std;
/// Graham Scan - Two Pass
/// Time Complexity: O(nlogn + 2n)
/// Space Complexity: O(1)
class Solution {
public:
vector<vector<int>> outerTrees(vector<vector<int>>& points) {
if(points.size() <= 3) return points;
sort(points.begin(), points.end(),
[](const vector<int>& p1, const vector<int>& p2){
return p1[0] == p2[0] ? p1[1] < p2[1] : p1[0] < p2[0];
});
vector<vector<int>> res;
for(vector<vector<int>>::iterator iter = points.begin(); iter != points.end(); iter ++){
while(res.size() >= 2){
int det = cross_value(res[res.size() - 2], res.back(), *iter);
if(det < 0)
res.pop_back();
else break;
}
res.push_back(*iter);
}
vector<vector<int>>::reverse_iterator riter = points.rbegin();
for(riter ++; riter != points.rend(); riter ++){
while(res.size() >= 2 && cross_value(res[res.size() - 2], res.back(), *riter) < 0)
res.pop_back();
res.push_back(*riter);
}
res.pop_back();
return res;
}
private:
int cross_value(const vector<int>& p1, const vector<int>& p2, const vector<int>& p3){
int a = p2[0] - p1[0], b = p2[1] - p1[1];
int c = p3[0] - p2[0], d = p3[1] - p2[1];
return a * d - b * c;
}
};
void print_vec(const vector<vector<int>>& vec){
for(const vector<int>& p: vec) cout << "(" << p[0] << "," << p[1] << ")"; cout << endl;
}
int main() {
vector<vector<int>> points = {{1,1},{2,2},{2,0},{2,4},{3,3},{4,2}};
print_vec(Solution().outerTrees(points));
return 0;
}