aboutsummaryrefslogtreecommitdiff
path: root/other/erect_fence.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/erect_fence.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/erect_fence.cpp')
-rw-r--r--other/erect_fence.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/other/erect_fence.cpp b/other/erect_fence.cpp
new file mode 100644
index 0000000..9d26fb9
--- /dev/null
+++ b/other/erect_fence.cpp
@@ -0,0 +1,29 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ using tree = vector<int>;
+ vector<tree> outerTrees(vector<tree> &trees) {
+ auto cross = [](tree &a, tree &b, tree &c) -> int {
+ return (c[1] - a[1]) * (a[0] - b[0]) - (a[1] - b[1]) * (c[0] - a[0]);
+ };
+ sort(trees.begin(), trees.end());
+ deque<tree> dv;
+ for (tree t : trees) {
+ while (dv.size() >= 2 &&
+ cross(dv[dv.size() - 1], dv[dv.size() - 2], t) < 0)
+ dv.pop_back();
+ dv.push_back(t);
+ while (dv.size() >= 2 && cross(dv[0], dv[1], t) > 0)
+ dv.pop_front();
+ dv.push_front(t);
+ }
+ set<tree> unique(dv.begin(), dv.end());
+ return vector<tree>(unique.begin(), unique.end());
+ }
+};
+
+int main() {
+ Solution obj;
+ vvd trees = {{1, 1}, {2, 2}, {2, 0}, {2, 4}, {3, 3}, {4, 2}};
+}