Problem solution · C++

Erect the Fence II

Erect the Fence II: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
105 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Erect the Fence II, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 105 lines of C++ from the credited upstream file 1924.cpp.
  • The implementation visibly relies on sequence storage.
  • 1 loop block detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeErect the Fence II · C++C++
Use this to learn the idea, then write your own version.
struct Point {  double x;  double y;  Point(double x, double y) : x(x), y(y) {}}; struct Disk {  Point center;  double radius;  Disk(const Point& center, double radius) : center(center), radius(radius) {}}; class Solution { public:  vector<double> outerTrees(vector<vector<int>>& trees) {    vector<Point> points;    for (int i = 0; i < trees.size(); ++i)      points.emplace_back(trees[i][0], trees[i][1]);    Disk disk = welzl(points, 0, {});    return {disk.center.x, disk.center.y, disk.radius};  }  private:  // Returns the smallest disk that encloses points[i..n).  //  // https://en.wikipedia.org/wiki/Smallest-disk_problem#Welzl's_algorithm  Disk welzl(const vector<Point>& points, int i, vector<Point> planePoints) {    if (i == points.size() || planePoints.size() == 3)      return trivial(planePoints);    Disk disk = welzl(points, i + 1, planePoints);    if (inside(disk, points[i]))      return disk;    return welzl(points, i + 1, addPlanePoint(planePoints, points[i]));  }   vector<Point> addPlanePoint(const vector<Point>& planePoints,                              const Point& point) {    vector<Point> newPlanePoints(planePoints);    newPlanePoints.push_back(point);    return newPlanePoints;  }  // Returns the smallest disk that encloses `planePoints`.  Disk trivial(const vector<Point>& planePoints) {    if (planePoints.empty())      return Disk(Point(0, 0), 0);    if (planePoints.size() == 1)      return Disk(Point(planePoints[0].x, planePoints[0].y), 0);    if (planePoints.size() == 2)      return getDisk(planePoints[0], planePoints[1]);     Disk disk01 = getDisk(planePoints[0], planePoints[1]);    if (inside(disk01, planePoints[2]))      return disk01;     Disk disk02 = getDisk(planePoints[0], planePoints[2]);    if (inside(disk02, planePoints[1]))      return disk02;     Disk disk12 = getDisk(planePoints[1], planePoints[2]);    if (inside(disk12, planePoints[0]))      return disk12;     return getDisk(planePoints[0], planePoints[1], planePoints[2]);  }   // Returns the smallest disk that encloses the points A and B.  Disk getDisk(const Point& A, const Point& B) {    const double x = (A.x + B.x) / 2;    const double y = (A.y + B.y) / 2;    return Disk(Point(x, y), distance(A, B) / 2);  }   // Returns the smallest disk that encloses the points A, B, and C.  Disk getDisk(const Point& A, const Point& B, const Point& C) {    // Calculate midpoints.    Point mAB((A.x + B.x) / 2, (A.y + B.y) / 2);    Point mBC((B.x + C.x) / 2, (B.y + C.y) / 2);     // Calculate the slopes and the perpendicular slopes.    const double slopeAB = (B.y - A.y) / (B.x - A.x);    const double slopeBC = (C.y - B.y) / (C.x - B.x);    const double perpSlopeAB = -1 / slopeAB;    const double perpSlopeBC = -1 / slopeBC;     // Calculate the center.    const double x =        (perpSlopeBC * mBC.x - perpSlopeAB * mAB.x + mAB.y - mBC.y) /        (perpSlopeBC - perpSlopeAB);    const double y = perpSlopeAB * (x - mAB.x) + mAB.y;    Point center(x, y);    return Disk(center, distance(center, A));  }   // Returns true if the point is inside the disk.  bool inside(Disk disk, Point point) {    return disk.radius > 0 && distance(disk.center, point) <= disk.radius;  }   double distance(Point A, Point B) {    const double dx = A.x - B.x;    const double dy = A.y - B.y;    return sqrt(dx * dx + dy * dy);  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗