Advertisement
imashutosh51

Erect the Fence(Monotone chain convex hull)

Nov 19th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. //https://www.youtube.com/watch?v=tAhT2qDgEyI
  2. /*
  3. Step 1: We will sort the array first on the basis of x then y.
  4. Step 2: so the first element will always be in fence because it's x and y both is less than others or one is same other is less.
  5. We will include first 2 points in the Uppr_tree eg. A and B so we will traverse the sorted points from 3 and if slope
  6. of A to C is greater than A to B then B should be removed else B should also be there and C should also be included.
  7. In other language,if new point is lying in left of previous two points eg. line from A to B then add C else remove B.
  8. Remove B until the size of rTree is not equal to 1 because first point will always be there because it's x and y is
  9. smaller than other points
  10.  
  11. step 3: step 2 for finding the Upper HULL ie. the points we will see from top
  12. so find the lower hull.Do same from last points
  13.  
  14. step4:merge upper and lower hull and remove the duplicate points
  15. */
  16.  
  17. class Solution {
  18. public:
  19.     int checkrightleft(vector<int> A, vector<int> B, vector<int> C) {  //((y3-y1/x3-x1) - (y2-y1)/(x2-x1)) if > 0
  20.                                                                        //means slope of AC is greater
  21.         return ((B[0] - A[0]) * (C[1] - A[1])) - ((B[1] - A[1]) * (C[0] - A[0]));
  22.     }
  23.    
  24.    
  25.     vector<vector<int>> outerTrees(vector<vector<int>>& trees) {
  26.         if (trees.size() <= 3) return trees;
  27.         sort(trees.begin(), trees.end());
  28.        
  29.         // Upper HULL
  30.         vector<vector<int>> Uppr_tree;
  31.         Uppr_tree.push_back(trees[0]);
  32.         Uppr_tree.push_back(trees[1]);
  33.          
  34.         for (int i = 2; i < trees.size(); i++) {
  35.          int ls= Uppr_tree.size();
  36.             while (Uppr_tree.size() >= 2 && checkrightleft(Uppr_tree[ls-2], Uppr_tree[ls-1], trees[i]) > 0) {
  37.                 Uppr_tree.pop_back();
  38.               ls--;
  39.             }
  40.             Uppr_tree.push_back(trees[i]);
  41.         }
  42.        
  43.         // Lower HULL
  44.         vector<vector<int>> rTrees;
  45.         rTrees.push_back(trees[trees.size() - 1]);
  46.         rTrees.push_back(trees[trees.size() - 2]);
  47.         for (int i = trees.size() - 3; i >= 0; --i) {
  48.             int rs = rTrees.size();
  49.             while (rTrees.size() >= 2 && checkrightleft(rTrees[rs-2], rTrees[rs-1], trees[i]) > 0) {
  50.                 rTrees.pop_back();
  51.                 rs--;
  52.             }
  53.             rTrees.push_back(trees[i]);
  54.         }
  55.          vector<vector<int>> answer(Uppr_tree.size()+rTrees.size());  //creating an array of combined size
  56.         merge(Uppr_tree.begin(),  //merging the array
  57.                    Uppr_tree.end(),
  58.                    rTrees.begin(),
  59.                    rTrees.end(),
  60.                    answer.begin());
  61.      
  62.        
  63.         sort(answer.begin(), answer.end());  //sorting the array
  64.         answer.erase(std::unique(answer.begin(), answer.end()), answer.end());  //removing common elements
  65.        
  66.         return answer;
  67.     }
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement