Problem solution · Java

Erect the Fence II

Erect the Fence II: a Java 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
108 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

  • 108 lines of Java from the credited upstream file 1924.java.
  • 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 · JavaJava
Use this to learn the idea, then write your own version.
class Point {  public double x;  public double y;  public Point(double x, double y) {    this.x = x;    this.y = y;  }} class Disk {  public Point center;  public double radius;  public Disk(Point center, double radius) {    this.center = center;    this.radius = radius;  }} class Solution {  public double[] outerTrees(int[][] trees) {    Point[] points = new Point[trees.length];    for (int i = 0; i < trees.length; ++i)      points[i] = new Point(trees[i][0], trees[i][1]);    Disk disk = welzl(points, 0, new ArrayList<>());    return new double[] {disk.center.x, disk.center.y, disk.radius};  }   // Returns the smallest disk that encloses points[i..n).  //  // https://en.wikipedia.org/wiki/Smallest-disk_problem#Welzl's_algorithm  private Disk welzl(Point[] points, int i, List<Point> planePoints) {    if (i == points.length || 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, addPlanePoints(planePoints, points[i]));  }   private List<Point> addPlanePoints(List<Point> planePoints, Point point) {    List<Point> newPlanePoints = new ArrayList<>(planePoints);    newPlanePoints.add(point);    return newPlanePoints;  }   // Returns the smallest disk that encloses `planePoints`.  private Disk trivial(List<Point> planePoints) {    if (planePoints.isEmpty())      return null;    if (planePoints.size() == 1)      return new Disk(new Point(planePoints.get(0).x, planePoints.get(0).y), 0);    if (planePoints.size() == 2)      return getDisk(planePoints.get(0), planePoints.get(1));     Disk disk01 = getDisk(planePoints.get(0), planePoints.get(1));    if (inside(disk01, planePoints.get(2)))      return disk01;     Disk disk02 = getDisk(planePoints.get(0), planePoints.get(2));    if (inside(disk02, planePoints.get(1)))      return disk02;     Disk disk12 = getDisk(planePoints.get(1), planePoints.get(2));    if (inside(disk12, planePoints.get(0)))      return disk12;     return getDisk(planePoints.get(0), planePoints.get(1), planePoints.get(2));  }   // Returns the smallest disk that encloses the points A and B.  private Disk getDisk(Point A, Point B) {    final double x = (A.x + B.x) / 2;    final double y = (A.y + B.y) / 2;    return new Disk(new Point(x, y), distance(A, B) / 2);  }   // Returns the smallest disk that encloses the points A, B, and C.  private Disk getDisk(Point A, Point B, Point C) {    // Calculate midpoints.    Point mAB = new Point((A.x + B.x) / 2, (A.y + B.y) / 2);    Point mBC = new Point((B.x + C.x) / 2, (B.y + C.y) / 2);     // Calculate the slopes and the perpendicular slopes.    final double slopeAB = (B.y - A.y) / (B.x - A.x);    final double slopeBC = (C.y - B.y) / (C.x - B.x);    final double perpSlopeAB = -1 / slopeAB;    final double perpSlopeBC = -1 / slopeBC;     // Calculate the center.    final double x =        (perpSlopeBC * mBC.x - perpSlopeAB * mAB.x + mAB.y - mBC.y) / (perpSlopeBC - perpSlopeAB);    final double y = perpSlopeAB * (x - mAB.x) + mAB.y;    Point center = new Point(x, y);    return new Disk(center, distance(center, A));  }   // Returns true if the point is inside the disk.  private boolean inside(Disk disk, Point point) {    return disk != null && distance(disk.center, point) <= disk.radius;  }   private double distance(Point A, Point B) {    final double dx = A.x - B.x;    final double dy = A.y - B.y;    return Math.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 ↗