Problem solution · Java

Maximum Number of Darts Inside of a Circular Dartboard

Maximum Number of Darts Inside of a Circular Dartboard: 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
51 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 Maximum Number of Darts Inside of a Circular Dartboard, 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

  • 51 lines of Java from the credited upstream file 1453.java.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks 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 codeMaximum Number of Darts Inside of a Circular Dartboard · JavaJava
Use this to learn the idea, then write your own version.
class Point {  public double x = 0;  public double y = 0;  public Point(double x, double y) {    this.x = x;    this.y = y;  }} class Solution {  public int numPoints(int[][] darts, int r) {    int ans = 1;    List<Point> points = convertToPoints(darts);     for (int i = 0; i < points.size(); ++i)      for (int j = i + 1; j < points.size(); ++j)        for (Point c : getCircles(points.get(i), points.get(j), r)) {          int count = 0;          for (Point point : points)            if (dist(c, point) - r <= ERR)              ++count;          ans = Math.max(ans, count);        }     return ans;  }   private static final double ERR = 1e-6;   private List<Point> convertToPoints(int[][] darts) {    List<Point> points = new ArrayList<>();    for (int[] dart : darts)      points.add(new Point(dart[0], dart[1]));    return points;  }   private Point[] getCircles(Point p, Point q, int r) {    if (dist(p, q) - 2.0 * r > ERR)      return new Point[] {};    Point m = new Point((p.x + q.x) / 2, (p.y + q.y) / 2);    final double distCM = Math.sqrt(Math.pow(r, 2) - Math.pow(dist(p, q) / 2, 2));    final double alpha = Math.atan2(p.y - q.y, q.x - p.x);    return new Point[] {new Point(m.x - distCM * Math.sin(alpha), m.y - distCM * Math.cos(alpha)),                        new Point(m.x + distCM * Math.sin(alpha), m.y + distCM * Math.cos(alpha))};  }   private double dist(Point p, Point q) {    return Math.sqrt(Math.pow(p.x - q.x, 2) + Math.pow(p.y - q.y, 2));  }} 

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 ↗