- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- 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.
Use this to learn the idea, then write your own version.
1struct Point {2 double x;3 double y;4 Point(double x, double y) : x(x), y(y) {}5};6 7class Solution {8 public:9 int numPoints(vector<vector<int>>& darts, int r) {10 int ans = 1;11 vector<Point> points = convertToPoints(darts);12 13 for (int i = 0; i < points.size(); ++i)14 for (int j = i + 1; j < points.size(); ++j)15 for (const Point& c : getCircles(points[i], points[j], r)) {16 int count = 0;17 for (const Point& point : points)18 if (dist(c, point) - r <= kErr)19 ++count;20 ans = max(ans, count);21 }22 23 return ans;24 }25 26 private:27 static constexpr double kErr = 1e-6;28 29 vector<Point> convertToPoints(const vector<vector<int>>& darts) {30 vector<Point> points;31 for (const vector<int>& dart : darts)32 points.emplace_back(dart[0], dart[1]);33 return points;34 }35 36 vector<Point> getCircles(const Point& p, const Point& q, int r) {37 if (dist(p, q) - 2.0 * r > kErr)38 return {};39 const Point m{(p.x + q.x) / 2, (p.y + q.y) / 2};40 const double distCM = sqrt(pow(r, 2) - pow(dist(p, q) / 2, 2));41 const double alpha = atan2(p.y - q.y, q.x - p.x);42 return {Point{m.x - distCM * sin(alpha), m.y - distCM * cos(alpha)},43 Point{m.x + distCM * sin(alpha), m.y + distCM * cos(alpha)}};44 }45 46 double dist(const Point& p, const Point& q) {47 return sqrt(pow(p.x - q.x, 2) + pow(p.y - q.y, 2));48 }49};50