From c065bd9577f107ade1e8e04f208e325197d2145b Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Tue, 20 Apr 2021 18:22:11 +0800 Subject: [PATCH] New Problem Solution - "1828. Queries on Number of Points Inside a Circle" --- README.md | 1 + .../QueriesOnNumberOfPointsInsideACircle.cpp | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 algorithms/cpp/queriesOnNumberOfPointsInsideACircle/QueriesOnNumberOfPointsInsideACircle.cpp diff --git a/README.md b/README.md index 0a1a2293f..ad045484b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|1828|[Queries on Number of Points Inside a Circle](https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/) | [C++](./algorithms/cpp/queriesOnNumberOfPointsInsideACircle/QueriesOnNumberOfPointsInsideACircle.cpp)|Medium| |1827|[Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/) | [C++](./algorithms/cpp/minimumOperationsToMakeTheArrayIncreasing/MinimumOperationsToMakeTheArrayIncreasing.cpp)|Easy| |1825|[Finding MK Average](https://leetcode.com/problems/finding-mk-average/) | [C++](./algorithms/cpp/findingMkAverage/FindingMkAverage.cpp)|Hard| |1824|[Minimum Sideway Jumps](https://leetcode.com/problems/minimum-sideway-jumps/) | [C++](./algorithms/cpp/minimumSidewayJumps/MinimumSidewayJumps.cpp)|Medium| diff --git a/algorithms/cpp/queriesOnNumberOfPointsInsideACircle/QueriesOnNumberOfPointsInsideACircle.cpp b/algorithms/cpp/queriesOnNumberOfPointsInsideACircle/QueriesOnNumberOfPointsInsideACircle.cpp new file mode 100644 index 000000000..c1c257fef --- /dev/null +++ b/algorithms/cpp/queriesOnNumberOfPointsInsideACircle/QueriesOnNumberOfPointsInsideACircle.cpp @@ -0,0 +1,72 @@ +// Source : https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/ +// Author : Hao Chen +// Date : 2021-04-20 + +/***************************************************************************************************** + * + * You are given an array points where points[i] = [xi, yi] is the coordinates of the i^th point on a + * 2D plane. Multiple points can have the same coordinates. + * + * You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at + * (xj, yj) with a radius of rj. + * + * For each query queries[j], compute the number of points inside the j^th circle. Points on the + * border of the circle are considered inside. + * + * Return an array answer, where answer[j] is the answer to the j^th query. + * + * Example 1: + * + * Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]] + * Output: [3,2,2] + * Explanation: The points and circles are shown above. + * queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle. + * + * Example 2: + * + * Input: points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]] + * Output: [2,3,2,4] + * Explanation: The points and circles are shown above. + * queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple. + * + * Constraints: + * + * 1 <= points.length <= 500 + * points[i].length == 2 + * 0 <= x​​​​​​i, y​​​​​​i <= 500 + * 1 <= queries.length <= 500 + * queries[j].length == 3 + * 0 <= xj, yj <= 500 + * 1 <= rj <= 500 + * All coordinates are integers. + * + * Follow up: Could you find the answer for each query in better complexity than O(n)? + ******************************************************************************************************/ + +class Solution { +private: + //refer to: https://stackoverflow.com/a/7227057/13139221 + bool inCircle( vector& point, vector& circle ) { + int x = point[0], y = point[1]; + int xo = circle[0], yo = circle[1], r = circle[2]; + + int dx = abs(x-xo); + if ( dx > r ) return false; + int dy = abs(y-yo); + if ( dy > r ) return false; + if ( dx + dy <= r ) return true; + return ( dx*dx + dy*dy <= r*r ); + } +public: + vector countPoints(vector>& points, vector>& queries) { + vector result; + for(auto& c : queries) { + int cnt = 0; + for(auto& p : points) { + if ( inCircle(p, c) ) cnt++; + } + result.push_back(cnt); + } + return result; + } +};