Problem solution · C++

Maximum Number of Darts Inside of a Circular Dartboard

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

  • 50 lines of C++ from the credited upstream file 1453.cpp.
  • 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 · 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) {}}; class Solution { public:  int numPoints(vector<vector<int>>& darts, int r) {    int ans = 1;    vector<Point> points = convertToPoints(darts);     for (int i = 0; i < points.size(); ++i)      for (int j = i + 1; j < points.size(); ++j)        for (const Point& c : getCircles(points[i], points[j], r)) {          int count = 0;          for (const Point& point : points)            if (dist(c, point) - r <= kErr)              ++count;          ans = max(ans, count);        }     return ans;  }  private:  static constexpr double kErr = 1e-6;   vector<Point> convertToPoints(const vector<vector<int>>& darts) {    vector<Point> points;    for (const vector<int>& dart : darts)      points.emplace_back(dart[0], dart[1]);    return points;  }   vector<Point> getCircles(const Point& p, const Point& q, int r) {    if (dist(p, q) - 2.0 * r > kErr)      return {};    const Point m{(p.x + q.x) / 2, (p.y + q.y) / 2};    const double distCM = sqrt(pow(r, 2) - pow(dist(p, q) / 2, 2));    const double alpha = atan2(p.y - q.y, q.x - p.x);    return {Point{m.x - distCM * sin(alpha), m.y - distCM * cos(alpha)},            Point{m.x + distCM * sin(alpha), m.y + distCM * cos(alpha)}};  }   double dist(const Point& p, const Point& q) {    return sqrt(pow(p.x - q.x, 2) + 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 ↗